Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Truncate.php
Go to the documentation of this file.
1 <?php
7 
18 {
22  protected $length;
23 
27  protected $etc;
28 
32  protected $remainder;
33 
37  protected $breakWords;
38 
42  protected $string;
43 
51  public function __construct(
52  \Magento\Framework\Stdlib\StringUtils $string,
53  $length = 80,
54  $etc = '...',
55  &$remainder = '',
56  $breakWords = true
57  ) {
58  $this->string = $string;
59  $this->length = $length;
60  $this->etc = $etc;
61  $this->remainder = & $remainder;
62  $this->breakWords = $breakWords;
63  }
64 
71  public function filter($string)
72  {
74  $this->remainder = '';
75  if (0 == $length) {
76  return '';
77  }
78 
79  $originalLength = $this->string->strlen($string);
80  if ($originalLength > $length) {
81  $length -= $this->string->strlen($this->etc);
82  if ($length <= 0) {
83  return '';
84  }
85  $preparedString = $string;
86  $preparedLength = $length;
87  if (!$this->breakWords) {
88  $preparedString = preg_replace('/\s+?(\S+)?$/u', '', $this->string->substr($string, 0, $length + 1));
89  $preparedLength = $this->string->strlen($preparedString);
90  }
91  $this->remainder = $this->string->substr($string, $preparedLength, $originalLength);
92  return $this->string->substr($preparedString, 0, $length) . $this->etc;
93  }
94 
95  return $string;
96  }
97 }
__construct(\Magento\Framework\Stdlib\StringUtils $string, $length=80, $etc='...', &$remainder='', $breakWords=true)
Definition: Truncate.php:51