Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields
StringUtils Class Reference

Public Member Functions

 upperCaseWords ($str, $sourceSeparator='_', $destinationSeparator='_')
 
 splitInjection ($str, $length=50, $needle='-', $insert=' ')
 
 split ($value, $length=1, $keepWords=false, $trim=false, $wordSeparatorRegex='\s')
 
 strlen ($string)
 
 cleanString ($string)
 
 substr ($string, $offset, $length=null)
 
 strrev ($str)
 
 strpos ($haystack, $needle, $offset=null)
 

Data Fields

const ICONV_CHARSET = 'UTF-8'
 

Detailed Description

Magento methods to work with string

@api

Since
100.0.2

Definition at line 14 of file StringUtils.php.

Member Function Documentation

◆ cleanString()

cleanString (   $string)

Clean non UTF-8 characters

Parameters
string$string
Returns
string

Definition at line 161 of file StringUtils.php.

162  {
163  return mb_convert_encoding($string, self::ICONV_CHARSET);
164  }

◆ split()

split (   $value,
  $length = 1,
  $keepWords = false,
  $trim = false,
  $wordSeparatorRegex = '\s' 
)

Binary-safe variant of strSplit()

  • option not to break words
  • option to trim spaces (between each word)
  • option to set character(s) (pcre pattern) to be considered as words separator
Parameters
string$value
int$length
bool$keepWords
bool$trim
string$wordSeparatorRegex
Returns
string[] @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Definition at line 75 of file StringUtils.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$count
Definition: recent.phtml:13
$value
Definition: gender.phtml:16
split($value, $length=1, $keepWords=false, $trim=false, $wordSeparatorRegex='\s')
Definition: StringUtils.php:75
$index
Definition: list.phtml:44
substr($string, $offset, $length=null)

◆ splitInjection()

splitInjection (   $str,
  $length = 50,
  $needle = '-',
  $insert = ' ' 
)

Split string and appending $insert string after $needle

Parameters
string$str
integer$length
string$needle
string$insert
Returns
string

Definition at line 43 of file StringUtils.php.

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  }
split($value, $length=1, $keepWords=false, $trim=false, $wordSeparatorRegex='\s')
Definition: StringUtils.php:75
strpos($haystack, $needle, $offset=null)
substr($string, $offset, $length=null)

◆ strlen()

strlen (   $string)

Retrieve string length using default charset

Parameters
string$string
Returns
int

Definition at line 150 of file StringUtils.php.

151  {
152  return mb_strlen($string, self::ICONV_CHARSET);
153  }

◆ strpos()

strpos (   $haystack,
  $needle,
  $offset = null 
)

Find position of first occurrence of a string

Parameters
string$haystack
string$needle
int$offset
Returns
int|bool

Definition at line 210 of file StringUtils.php.

211  {
212  return mb_strpos($haystack, $needle, $offset, self::ICONV_CHARSET);
213  }

◆ strrev()

strrev (   $str)

Binary-safe strrev()

Parameters
string$str
Returns
string

Definition at line 189 of file StringUtils.php.

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  }
$i
Definition: gallery.phtml:31
substr($string, $offset, $length=null)

◆ substr()

substr (   $string,
  $offset,
  $length = null 
)

Pass through to mb_substr()

Parameters
string$string
int$offset
int$length
Returns
string

Definition at line 174 of file StringUtils.php.

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  }

◆ upperCaseWords()

upperCaseWords (   $str,
  $sourceSeparator = '_',
  $destinationSeparator = '_' 
)

Capitalize first letters and convert separators if needed

Parameters
string$str
string$sourceSeparator
string$destinationSeparator
Returns
string

Definition at line 29 of file StringUtils.php.

30  {
31  return str_replace(' ', $destinationSeparator, ucwords(str_replace($sourceSeparator, ' ', $str)));
32  }

Field Documentation

◆ ICONV_CHARSET

const ICONV_CHARSET = 'UTF-8'

Default charset

Definition at line 19 of file StringUtils.php.


The documentation for this class was generated from the following file: