Came from http://j-reaux.developpez.com/tutoriel/php/fonctions-troncature-texte/.

@param string $text
@param int    $nbCaracter
@param string $readMoreText

@return string
541862
public static function resumeHtml($text, $nbCaracter, $readMoreText = '')
    {
        if (is_numeric($nbCaracter)) {
            $lengthBeforeWithoutHtml = strlen(trim(strip_tags($text)));
            $htmlSplitMask = '#</?([a-zA-Z1-6]+)(?: +[a-zA-Z]+="[^"]*")*( ?/)?>#';
            $htmlMatchMask = '#<(?:/([a-zA-Z1-6]+)|([a-zA-Z1-6]+)(?: +[a-zA-Z]+="[^"]*")*( ?/)?)>#';
            $text                   .= ' ';
            $textPieces = preg_split($htmlSplitMask, $text, -1, PREG_SPLIT_OFFSET_CAPTURE | PREG_SPLIT_NO_EMPTY);
            $pieceNumber = count($textPieces);

            if ($pieceNumber == 1) {
                $text        .= ' ';
                $lenghtBefore = strlen($text);
                $text = substr($text, 0, strpos($text, ' ', $lenghtBefore > $nbCaracter ? $nbCaracter : $lenghtBefore));

                if ($readMoreText != '' && $lenghtBefore > $nbCaracter) {
                    $text .= $readMoreText;
                }
            } else {
                $length = 0;
                $indexLastPiece = $pieceNumber - 1;
                $position = $textPieces[$indexLastPiece][1] + strlen($textPieces[$indexLastPiece][0]) - 1;
                $indexPiece = $indexLastPiece;
                $searchSpace = true;

                foreach ($textPieces as $index => $bout) {
                    $length += strlen($bout[0]);
                    if ($length >= $nbCaracter) {
                        $positionEndPiece = $bout[1] + strlen($bout[0]) - 1;
                        $position = $positionEndPiece - ($length - $nbCaracter);
                        if (($positionSpace = strpos($bout[0], ' ', $position - $bout[1])) !== false) {
                            $position = $bout[1] + $positionSpace;
                            $searchSpace = false;
                        }
                        if ($index != $indexLastPiece) {
                            $indexPiece = $index + 1;
                        }
                        break;
                    }
                }

                if ($searchSpace === true) {
                    for ($i = $indexPiece; $i <= $indexLastPiece; ++$i) {
                        $position = $textPieces[$i][1];
                        if (($positionSpace = strpos($textPieces[$i][0], ' ')) !== false) {
                            $position += $positionSpace;
                            break;
                        }
                    }
                }

                $text = substr($text, 0, $position);
                preg_match_all($htmlMatchMask, $text, $return, PREG_OFFSET_CAPTURE);
                $tagPieces = array();

                foreach ($return[0] as $index => $tag) {
                    if (isset($return[3][$index][0])) {
                        continue;
                    }
                    if ($return[0][$index][0][1] != '/') {
                        array_unshift($tagPieces, $return[2][$index][0]);
                    } else {
                        array_shift($tagPieces);
                    }
                }

                if (!empty($tagPieces)) {
                    foreach ($tagPieces as $tag) {
                        $text .= '</'.$tag.'>';
                    }
                }

                if ($readMoreText != '' && $lengthBeforeWithoutHtml > $nbCaracter) {
                    $text   .= 'SuspensionPoint';
                    $pattern = '#((</[^>]*>[\n\t\r ]*)?(</[^>]*>[\n\t\r ]*)?(</[^>]*>[\n\t\r ]*)?(</[^>]*>[\n\t\r ]*)?(</[^>]*>)[\n\t\r ]*SuspensionPoint)#i';
                    $text = preg_replace($pattern, $readMoreText.'${2}${3}${4}${5}${6}', $text);
                }
            }
        }

        return $text;
    }
1085048
public function process($text)
    {
        $iterations = $this->maxIterations === null ? 1 : $this->maxIterations;
        $context = new ProcessorContext();
        $context->processor = $this;

        while ($iterations--) {
            $context->iterationNumber++;
            $newText = $this->processIteration($text, $context, null);
            if ($newText === $text) {
                break;
            }
            $text = $newText;
            $iterations += $this->maxIterations === null ? 1 : 0;
        }

        return $text;
    }
1138127
public function convert($text, $toEncoding, $fromEncoding = null)
    {
        return $this->findReturningTrueOrFail('canConvert', $toEncoding)
                    ->convert($text, $toEncoding, $fromEncoding);
    }
276128
public function readTab($text) {
		$pieces = explode("\n", $text);
		$result = [];
		foreach ($pieces as $piece) {
			$tmp = explode("\t", trim($piece, "\r\n"));
			$result[] = $tmp;
		}
		return $result;
	}
1122221
public function parse(string $text) : \stdClass
    {
        $items = Text::split($text, $this->separator);
        $itemsCount = count($items);
        $itemIndex = 0;
        $names = array_keys($this->fields);
        $namesCount = count($names);
        $nameIndex = 0;
        $index = 0;
        $separatorLength = strlen($this->separator);
        $itemsLeft = false;
        $this->clearValues();
        try {
            while ($itemIndex < $itemsCount && $nameIndex < $namesCount) {
                $syntax = $this->fields[$names[$nameIndex]];
                $this->values[$names[$nameIndex]]->value = $syntax->parse($items[$itemIndex]);
                $this->values[$names[$nameIndex]]->error = null;
                $index += strlen($items[$itemIndex]) + $separatorLength;
                $nameIndex ++;
                $itemIndex ++;
                if ($syntax instanceof OptionalSyntax && !$syntax->success()) {
                    $itemIndex --;
                }
            }
            // if items are left
            if ($itemIndex < $itemsCount)
                $itemsLeft = true;
            // if fields are left
            while ($nameIndex < $namesCount) {
                $syntax = $this->fields[$names[$nameIndex]];
                $this->values[$names[$nameIndex]]->value = $syntax->parse('');
                $this->values[$names[$nameIndex]]->error = null;
                $nameIndex ++;
            }
        } catch (ParseException $e) {
            if ($itemIndex < $itemsCount) {
                $this->values[$names[$nameIndex]]->error = static::INVALID_FIELD;
                $error = "Unable to parse the item '{$items[$itemIndex]}' for field '{$names[$nameIndex]}'";
                $extra = [
                    'type'  => 'invalid-field',
                    'field' => $names[$nameIndex],
                    'text'  => $items[$itemIndex]
                ];
            } else {
                $this->values[$names[$nameIndex]]->error = static::MISSING_FIELD;
                $error = "No item left for field '{$names[$nameIndex]}'";
                $extra = [
                    'type'  => 'missing-field',
                    'field' => $names[$nameIndex],
                    'position' => $e->position()
                ];
            }
            throw new ParseException($this, $text, $index + $e->position(), $error, $extra, $e);
        }

        if ($itemsLeft) {
            $extra = [
                'type'  => 'additional-items',
                'items' => array_slice($items, $itemIndex)
            ];
            throw new ParseException($this, $text, $index - $separatorLength,
                "Additional items with no corresponding fields", $extra);
        }

        return (object) array_map(function(\stdClass $field) {
            return $field->value;
        }, $this->values);
    }
1039205
public function waitForVisibleText($text)
    {
        $this->waitFor(function ($context) use ($text) {
            try {
                /** @var Web $context */
                $context->assertPageContainsText($text);
                return true;
            } catch (ResponseTextException $e) {

            }
        });
    }
151642
public static function strtolower($text, $charset='utf-8') {
        $charset = self::parse_charset($charset);

        if ($charset === 'utf-8' and function_exists('mb_strtolower')) {
            return mb_strtolower($text, 'UTF-8');
        }

        $oldlevel = error_reporting(E_PARSE);
        $result = self::typo3()->conv_case($charset, (string)$text, 'toLower');
        error_reporting($oldlevel);

        return $result;
    }
1117137
private function addTranslatable($text)
    {
        if (strlen($text) > 1 && !isset($this->translatables[$text])) {
            $this->translatables[$text] = '';
        }
    }
350733
private function getSequenceParsed($text, $currentMode) {
        if ($this->tilde) {
            $sequence = '';
            $previousPos = 0;
            while (($pos = strpos($text, '~', $previousPos)) !== false) {
                $tildeData = self::extractTilde($text, $pos);

                $simpleTilde = ($tildeData === '~~');
                if ($simpleTilde && $currentMode !== 'B') {
                    throw new CINParseException('code128', 'The Table ' . $currentMode . ' doesn\'t contain the character ~.');
                }

                // At this point, we know we have ~Fx
                if ($tildeData !== '~F1' && $currentMode === 'C') {
                    // The mode C doesn't support ~F2, ~F3, ~F4
                    throw new CINParseException('code128', 'The Table C doesn\'t contain the function ' . $tildeData . '.');
                }

                $length = $pos - $previousPos;
                if ($currentMode === 'C') {
                    if ($length % 2 === 1) {
                        throw new CINParseException('code128', 'The text "' . $text . '" must have an even number of character to be encoded in Table C.');
                    }
                }

                $sequence .= str_repeat('.', $length);
                $sequence .= '.';
                $sequence .= (!$simpleTilde) ? 'F' : '';
                $previousPos = $pos + strlen($tildeData);
            }

            // Flushing
            $length = strlen($text) - $previousPos;
            if ($currentMode === 'C') {
                if ($length % 2 === 1) {
                    throw new CINParseException('code128', 'The text "' . $text . '" must have an even number of character to be encoded in Table C.');
                }
            }

            $sequence .= str_repeat('.', $length);

            return $sequence;
        } else {
            return str_repeat('.', strlen($text));
        }
    }
711069
private function setParseB($text, &$currentMode) {
        $tmp = preg_quote($this->keysB, '/');

        $match = array();
        if (preg_match('/[^' . $tmp . ']/', $text, $match) === 1) {
            // We found something not allowed
            throw new CINParseException('code128', 'The text "' . $text . '" can\'t be parsed with the Table B. The character "' . $match[0] . '" is not allowed.');
        } else {
            $latch = ($currentMode === 'B') ? '' : '1';
            $currentMode = 'B';

            return $latch . $this->getSequenceParsed($text, $currentMode);
        }
    }
1208036
public function textToSpeech(string $text): string
    {
        $result = $this->sendRequest("https://api.voicerss.org/", [
            "key"   =>  $this->apikey,
            "src"   =>  $text,
            "hl"    =>  $this->language,
            "r"     =>  (string) $this->speed,
            "c"     =>  "MP3",
            "f"     =>  "16khz_16bit_stereo",
        ]);

        if (substr($result, 0, 6) === "ERROR:") {
            throw new ProviderException("TextToSpeech {$result}");
        }

        return $result;
    }
1280382
public function stderr(string $text = '', $nl = true): int
    {
        return Console::write($text, $nl, [
            'steam' => $this->errorStream,
        ]);
    }
213267
public static function stdout($text, $raw = false)
    {
        if ($raw) {
            return fwrite(STDOUT, $text);
        } elseif (extension_loaded('posix') && posix_isatty(STDOUT)) {
            return fwrite(STDOUT, static::colorize($text));
        } else {
            return fwrite(STDOUT, static::decolorize($text));
        }
    }
243742
public function read(&$text)
    {
        /**
         * We could make a better effort at only finding @ between <?php ?>
         * but that's probably not necessary as @ doesn't occur much in the wild
         * and there's a significant performance gain by using str_replace().
         */

        // Replace \@ with \$
        $text = str_replace('\@', '\$', $text);

        // Now replace non-eescaped @'s
         $text = str_replace(array('@$'), '$', $text);

        // Replace \$ with @
        $text = str_replace('\$', '@', $text);

        return $this;
    }
294515
public static function maxLen($text, $maxLen)
	{
	    if (is_array($maxLen)) {
	        if (!isset($maxLen['maxLen'])) {
	            throw new \InvalidArgumentException('The $minLen argument must have "maxLen" key');
	        }
	        $maxLen = $maxLen['maxLen'];
	    }

	    return strlen($text) <= $maxLen;
	}
40295
public function setText(string $text)
    {
        $this
            ->getScanner()
            ->setText($text);
        $this->buffer = null;
        return $this;
    }
67267
public function toMorse($text) {
        $text = strtoupper($text);
        $words = preg_split('#\s+#', $text);
        $morse = array_map([$this, 'morseWord'], $words);
        return implode($this->wordSeparator, $morse);
    }