1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Stdlib;
/**
* Magento methods to work with string
*
* @api
* @since 100.0.2
*/
class StringUtils
{
/**
* Default charset
*/
const ICONV_CHARSET = 'UTF-8';
/**
* Capitalize first letters and convert separators if needed
*
* @param string $str
* @param string $sourceSeparator
* @param string $destinationSeparator
* @return string
*/
public function upperCaseWords($str, $sourceSeparator = '_', $destinationSeparator = '_')
{
return str_replace(' ', $destinationSeparator, ucwords(str_replace($sourceSeparator, ' ', $str)));
}
/**
* Split string and appending $insert string after $needle
*
* @param string $str
* @param integer $length
* @param string $needle
* @param string $insert
* @return string
*/
public function splitInjection($str, $length = 50, $needle = '-', $insert = ' ')
{
$str = $this->split($str, $length);
$newStr = '';
foreach ($str as $part) {
if ($this->strlen($part) >= $length) {
$lastDelimiter = $this->strpos($this->strrev($part), $needle);
$tmpNewStr = $this->substr($this->strrev($part), 0, $lastDelimiter) . $insert
. $this->substr($this->strrev($part), $lastDelimiter);
$newStr .= $this->strrev($tmpNewStr);
} else {
$newStr .= $part;
}
}
return $newStr;
}
/**
* 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
*
* @param string $value
* @param int $length
* @param bool $keepWords
* @param bool $trim
* @param string $wordSeparatorRegex
* @return string[]
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function split($value, $length = 1, $keepWords = false, $trim = false, $wordSeparatorRegex = '\s')
{
$result = [];
$strLen = $this->strlen($value);
if (!$strLen || !is_int($length) || $length <= 0) {
return $result;
}
if ($trim) {
$value = trim(preg_replace('/\s{2,}/siu', ' ', $value));
}
// do a usual str_split, but safe for our encoding
if (!$keepWords || $length < 2) {
for ($offset = 0; $offset < $strLen; $offset += $length) {
$result[] = $this->substr($value, $offset, $length);
}
} else {
// split smartly, keeping words
$split = preg_split('/(' . $wordSeparatorRegex . '+)/siu', $value, null, PREG_SPLIT_DELIM_CAPTURE);
$index = 0;
$space = '';
$spaceLen = 0;
foreach ($split as $key => $part) {
if ($trim) {
// ignore spaces (even keys)
if ($key % 2) {
continue;
}
$space = ' ';
$spaceLen = 1;
}
if (empty($result[$index])) {
$currentLength = 0;
$result[$index] = '';
$space = '';
$spaceLen = 0;
} else {
$currentLength = $this->strlen($result[$index]);
}
$partLength = $this->strlen($part);
// add part to current last element
if ($currentLength + $spaceLen + $partLength <= $length) {
$result[$index] .= $space . $part;
} elseif ($partLength <= $length) {
// add part to new element
$index++;
$result[$index] = $part;
} else {
// break too long part recursively
foreach ($this->split($part, $length, false, $trim, $wordSeparatorRegex) as $subPart) {
$index++;
$result[$index] = $subPart;
}
}
}
}
// remove last element, if empty
$count = count($result);
if ($count) {
if ($result[$count - 1] === '') {
unset($result[$count - 1]);
}
}
// remove first element, if empty
if (isset($result[0]) && $result[0] === '') {
array_shift($result);
}
return $result;
}
/**
* Retrieve string length using default charset
*
* @param string $string
* @return int
*/
public function strlen($string)
{
return mb_strlen($string, self::ICONV_CHARSET);
}
/**
* Clean non UTF-8 characters
*
* @param string $string
* @return string
*/
public function cleanString($string)
{
return mb_convert_encoding($string, self::ICONV_CHARSET);
}
/**
* Pass through to mb_substr()
*
* @param string $string
* @param int $offset
* @param int $length
* @return string
*/
public function substr($string, $offset, $length = null)
{
$string = $this->cleanString($string);
if ($length === null) {
$length = $this->strlen($string) - $offset;
}
return mb_substr($string, $offset, $length, self::ICONV_CHARSET);
}
/**
* Binary-safe strrev()
*
* @param string $str
* @return string
*/
public function strrev($str)
{
$result = '';
$strLen = $this->strlen($str);
if (!$strLen) {
return $result;
}
for ($i = $strLen - 1; $i >= 0; $i--) {
$result .= $this->substr($str, $i, 1);
}
return $result;
}
/**
* Find position of first occurrence of a string
*
* @param string $haystack
* @param string $needle
* @param int $offset
* @return int|bool
*/
public function strpos($haystack, $needle, $offset = null)
{
return mb_strpos($haystack, $needle, $offset, self::ICONV_CHARSET);
}
}