Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StringUtils.php
Go to the documentation of this file.
1 <?php
7 
15 {
19  const ICONV_CHARSET = 'UTF-8';
20 
29  public function upperCaseWords($str, $sourceSeparator = '_', $destinationSeparator = '_')
30  {
31  return str_replace(' ', $destinationSeparator, ucwords(str_replace($sourceSeparator, ' ', $str)));
32  }
33 
43  public function splitInjection($str, $length = 50, $needle = '-', $insert = ' ')
44  {
45  $str = $this->split($str, $length);
46  $newStr = '';
47  foreach ($str as $part) {
48  if ($this->strlen($part) >= $length) {
49  $lastDelimiter = $this->strpos($this->strrev($part), $needle);
50  $tmpNewStr = $this->substr($this->strrev($part), 0, $lastDelimiter) . $insert
51  . $this->substr($this->strrev($part), $lastDelimiter);
52  $newStr .= $this->strrev($tmpNewStr);
53  } else {
54  $newStr .= $part;
55  }
56  }
57  return $newStr;
58  }
59 
75  public function split($value, $length = 1, $keepWords = false, $trim = false, $wordSeparatorRegex = '\s')
76  {
77  $result = [];
78  $strLen = $this->strlen($value);
79  if (!$strLen || !is_int($length) || $length <= 0) {
80  return $result;
81  }
82  if ($trim) {
83  $value = trim(preg_replace('/\s{2,}/siu', ' ', $value));
84  }
85  // do a usual str_split, but safe for our encoding
86  if (!$keepWords || $length < 2) {
87  for ($offset = 0; $offset < $strLen; $offset += $length) {
88  $result[] = $this->substr($value, $offset, $length);
89  }
90  } else {
91  // split smartly, keeping words
92  $split = preg_split('/(' . $wordSeparatorRegex . '+)/siu', $value, null, PREG_SPLIT_DELIM_CAPTURE);
93  $index = 0;
94  $space = '';
95  $spaceLen = 0;
96  foreach ($split as $key => $part) {
97  if ($trim) {
98  // ignore spaces (even keys)
99  if ($key % 2) {
100  continue;
101  }
102  $space = ' ';
103  $spaceLen = 1;
104  }
105  if (empty($result[$index])) {
106  $currentLength = 0;
107  $result[$index] = '';
108  $space = '';
109  $spaceLen = 0;
110  } else {
111  $currentLength = $this->strlen($result[$index]);
112  }
113  $partLength = $this->strlen($part);
114  // add part to current last element
115  if ($currentLength + $spaceLen + $partLength <= $length) {
116  $result[$index] .= $space . $part;
117  } elseif ($partLength <= $length) {
118  // add part to new element
119  $index++;
120  $result[$index] = $part;
121  } else {
122  // break too long part recursively
123  foreach ($this->split($part, $length, false, $trim, $wordSeparatorRegex) as $subPart) {
124  $index++;
125  $result[$index] = $subPart;
126  }
127  }
128  }
129  }
130  // remove last element, if empty
131  $count = count($result);
132  if ($count) {
133  if ($result[$count - 1] === '') {
134  unset($result[$count - 1]);
135  }
136  }
137  // remove first element, if empty
138  if (isset($result[0]) && $result[0] === '') {
139  array_shift($result);
140  }
141  return $result;
142  }
143 
150  public function strlen($string)
151  {
152  return mb_strlen($string, self::ICONV_CHARSET);
153  }
154 
161  public function cleanString($string)
162  {
163  return mb_convert_encoding($string, self::ICONV_CHARSET);
164  }
165 
174  public function substr($string, $offset, $length = null)
175  {
176  $string = $this->cleanString($string);
177  if ($length === null) {
178  $length = $this->strlen($string) - $offset;
179  }
180  return mb_substr($string, $offset, $length, self::ICONV_CHARSET);
181  }
182 
189  public function strrev($str)
190  {
191  $result = '';
192  $strLen = $this->strlen($str);
193  if (!$strLen) {
194  return $result;
195  }
196  for ($i = $strLen - 1; $i >= 0; $i--) {
197  $result .= $this->substr($str, $i, 1);
198  }
199  return $result;
200  }
201 
210  public function strpos($haystack, $needle, $offset = null)
211  {
212  return mb_strpos($haystack, $needle, $offset, self::ICONV_CHARSET);
213  }
214 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$count
Definition: recent.phtml:13
upperCaseWords($str, $sourceSeparator='_', $destinationSeparator='_')
Definition: StringUtils.php:29
$value
Definition: gender.phtml:16
split($value, $length=1, $keepWords=false, $trim=false, $wordSeparatorRegex='\s')
Definition: StringUtils.php:75
strpos($haystack, $needle, $offset=null)
splitInjection($str, $length=50, $needle='-', $insert=' ')
Definition: StringUtils.php:43
$i
Definition: gallery.phtml:31
$index
Definition: list.phtml:44
substr($string, $offset, $length=null)