‪TYPO3CMS  ‪main
TextCropper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 {
34  public function ‪crop(string $content, int $numberOfChars, string $replacementForEllipsis, bool $cropToSpace): string
35  {
36  if (!$numberOfChars || !(mb_strlen($content, 'utf-8') > abs($numberOfChars))) {
37  return $content;
38  }
39 
40  if ($numberOfChars < 0) {
41  // cropping from the right side of the content, prepanding replacementForEllipsis
42  $content = mb_substr($content, $numberOfChars, null, 'utf-8');
43  $truncatePosition = $cropToSpace ? mb_strpos($content, ' ', 0, 'utf-8') : false;
44  return $truncatePosition > 0
45  ? $replacementForEllipsis . mb_substr($content, $truncatePosition, null, 'utf-8')
46  : $replacementForEllipsis . $content;
47  }
48 
49  // cropping from the left side of content, appending replacementForEllipsis
50  $content = mb_substr($content, 0, $numberOfChars, 'utf-8');
51  $truncatePosition = $cropToSpace ? mb_strrpos($content, ' ', 0, 'utf-8') : false;
52  return $truncatePosition > 0
53  ? mb_substr($content, 0, $truncatePosition, 'utf-8') . $replacementForEllipsis
54  : $content . $replacementForEllipsis;
55  }
56 }
‪TYPO3\CMS\Core\Text\TextCropper\crop
‪string crop(string $content, int $numberOfChars, string $replacementForEllipsis, bool $cropToSpace)
Definition: TextCropper.php:34
‪TYPO3\CMS\Core\Text
Definition: TextCropper.php:18
‪TYPO3\CMS\Core\Text\TextCropper
Definition: TextCropper.php:21