1: <?php
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: class Mage_Core_Helper_String extends Mage_Core_Helper_Abstract
33: {
34: const ICONV_CHARSET = 'UTF-8';
35:
36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46:
47: public function truncate($string, $length = 80, $etc = '...', &$remainder = '', $breakWords = true)
48: {
49: $remainder = '';
50: if (0 == $length) {
51: return '';
52: }
53:
54: $originalLength = $this->strlen($string);
55: if ($originalLength > $length) {
56: $length -= $this->strlen($etc);
57: if ($length <= 0) {
58: return '';
59: }
60: $preparedString = $string;
61: $preparedlength = $length;
62: if (!$breakWords) {
63: $preparedString = preg_replace('/\s+?(\S+)?$/u', '', $this->substr($string, 0, $length + 1));
64: $preparedlength = $this->strlen($preparedString);
65: }
66: $remainder = $this->substr($string, $preparedlength, $originalLength);
67: return $this->substr($preparedString, 0, $length) . $etc;
68: }
69:
70: return $string;
71: }
72:
73: 74: 75: 76: 77: 78:
79: public function strlen($string)
80: {
81: return iconv_strlen($string, self::ICONV_CHARSET);
82: }
83:
84: 85: 86: 87: 88: 89: 90: 91:
92: public function substr($string, $offset, $length = null)
93: {
94: $string = $this->cleanString($string);
95: if (is_null($length)) {
96: $length = $this->strlen($string) - $offset;
97: }
98: return iconv_substr($string, $offset, $length, self::ICONV_CHARSET);
99: }
100:
101: 102: 103: 104: 105: 106: 107: 108: 109:
110: public function splitInjection($str, $length = 50, $needle = '-', $insert = ' ')
111: {
112: $str = $this->str_split($str, $length);
113: $newStr = '';
114: foreach ($str as $part) {
115: if ($this->strlen($part) >= $length) {
116: $lastDelimetr = $this->strpos($this->strrev($part), $needle);
117: $tmpNewStr = '';
118: $tmpNewStr = $this->substr($this->strrev($part), 0, $lastDelimetr)
119: . $insert . $this->substr($this->strrev($part), $lastDelimetr);
120: $newStr .= $this->strrev($tmpNewStr);
121: } else {
122: $newStr .= $part;
123: }
124: }
125: return $newStr;
126: }
127:
128: 129: 130: 131: 132: 133:
134: public function strrev($str)
135: {
136: $result = '';
137: $strlen = $this->strlen($str);
138: if (!$strlen) {
139: return $result;
140: }
141: for ($i = $strlen-1; $i >= 0; $i--) {
142: $result .= $this->substr($str, $i, 1);
143: }
144: return $result;
145: }
146:
147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159:
160: public function str_split($str, $length = 1, $keepWords = false, $trim = false, $wordSeparatorRegex = '\s')
161: {
162: $result = array();
163: $strlen = $this->strlen($str);
164: if ((!$strlen) || (!is_int($length)) || ($length <= 0)) {
165: return $result;
166: }
167:
168: if ($trim) {
169: $str = trim(preg_replace('/\s{2,}/siu', ' ', $str));
170: }
171:
172: if ((!$keepWords) || ($length < 2)) {
173: for ($offset = 0; $offset < $strlen; $offset += $length) {
174: $result[] = $this->substr($str, $offset, $length);
175: }
176: }
177:
178: else {
179: $split = preg_split('/(' . $wordSeparatorRegex . '+)/siu', $str, null, PREG_SPLIT_DELIM_CAPTURE);
180: $i = 0;
181: $space = '';
182: $spaceLen = 0;
183: foreach ($split as $key => $part) {
184: if ($trim) {
185:
186: if ($key % 2) {
187: continue;
188: }
189: $space = ' ';
190: $spaceLen = 1;
191: }
192: if (empty($result[$i])) {
193: $currentLength = 0;
194: $result[$i] = '';
195: $space = '';
196: $spaceLen = 0;
197: }
198: else {
199: $currentLength = $this->strlen($result[$i]);
200: }
201: $partLength = $this->strlen($part);
202:
203: if (($currentLength + $spaceLen + $partLength) <= $length) {
204: $result[$i] .= $space . $part;
205: }
206:
207: elseif ($partLength <= $length) {
208: $i++;
209: $result[$i] = $part;
210: }
211:
212: else {
213: foreach ($this->str_split($part, $length, false, $trim, $wordSeparatorRegex) as $subpart) {
214: $i++;
215: $result[$i] = $subpart;
216: }
217: }
218: }
219: }
220:
221: if ($count = count($result)) {
222: if ($result[$count - 1] === '') {
223: unset($result[$count - 1]);
224: }
225: }
226:
227: if (isset($result[0]) && $result[0] === '') {
228: array_shift($result);
229: }
230: return $result;
231: }
232:
233: 234: 235: 236: 237: 238: 239: 240: 241:
242: function splitWords($str, $uniqueOnly = false, $maxWordLength = 0, $wordSeparatorRegexp = '\s')
243: {
244: $result = array();
245: $split = preg_split('#' . $wordSeparatorRegexp . '#siu', $str, null, PREG_SPLIT_NO_EMPTY);
246: foreach ($split as $word) {
247: if ($uniqueOnly) {
248: $result[$word] = $word;
249: }
250: else {
251: $result[] = $word;
252: }
253: }
254: if ($maxWordLength && count($result) > $maxWordLength) {
255: $result = array_slice($result, 0, $maxWordLength);
256: }
257: return $result;
258: }
259:
260: 261: 262: 263: 264: 265:
266: public function cleanString($string)
267: {
268: return '"libiconv"' == ICONV_IMPL ?
269: iconv(self::ICONV_CHARSET, self::ICONV_CHARSET . '//IGNORE', $string) : $string;
270: }
271:
272: 273: 274: 275: 276: 277: 278: 279:
280: public function strpos($haystack, $needle, $offset = null)
281: {
282: return iconv_strpos($haystack, $needle, $offset, self::ICONV_CHARSET);
283: }
284:
285: 286: 287: 288: 289: 290:
291: public function ksortMultibyte(array &$sort)
292: {
293: if (empty($sort)) {
294: return false;
295: }
296: $oldLocale = setlocale(LC_COLLATE, "0");
297: $localeCode = Mage::app()->getLocale()->getLocaleCode();
298:
299: setlocale(LC_COLLATE, $localeCode . '.UTF8', 'C.UTF-8', 'en_US.utf8');
300: ksort($sort, SORT_LOCALE_STRING);
301: setlocale(LC_COLLATE, $oldLocale);
302:
303: return $sort;
304: }
305:
306: }
307: