/** * Crop a string to ``maximumCharacters`` length, taking sentences into account, * optionally appending ``suffix`` if cropping was necessary. * * @param string $string The input string * @param integer $maximumCharacters Number of characters where cropping should happen * @param string $suffix Suffix to be appended if cropping was necessary * @return string The cropped string */ public function cropAtSentence($string, $maximumCharacters, $suffix = '') { if (UnicodeFunctions::strlen($string) > $maximumCharacters) { $iterator = new TextIterator($string, TextIterator::SENTENCE); $string = UnicodeFunctions::substr($string, 0, $iterator->preceding($maximumCharacters)); $string .= $suffix; } return $string; }
/** * Checks if the "preceding" method basically works with word iteration * * @test */ public function precedingBasicallyWorks() { $iterator = new TextIterator('This is a test string. Let\'s iterate it by word', TextIterator::WORD); $this->assertEquals($iterator->preceding(11), 10, 'Wrong offset for the preceding element returned.' . $iterator->preceding(11)); }