00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 class Mage_Core_Helper_String extends Mage_Core_Helper_Abstract
00033 {
00034 const ICONV_CHARSET = 'UTF-8';
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 public function truncate($string, $length = 80, $etc = '...', &$remainder = '', $breakWords = true)
00048 {
00049 $remainder = '';
00050 if (0 == $length) {
00051 return '';
00052 }
00053
00054 $originalLength = iconv_strlen($string, self::ICONV_CHARSET);
00055 if ($originalLength > $length) {
00056 $length -= iconv_strlen($etc, self::ICONV_CHARSET);
00057 if ($length <= 0) {
00058 return '';
00059 }
00060 $preparedString = $string;
00061 $preparedlength = $length;
00062 if (!$breakWords) {
00063 $preparedString = preg_replace('/\s+?(\S+)?$/', '', iconv_substr($string, 0, $length + 1, self::ICONV_CHARSET));
00064 $preparedlength = iconv_strlen($preparedString, self::ICONV_CHARSET);
00065 }
00066 $remainder = iconv_substr($string, $preparedlength, $originalLength, self::ICONV_CHARSET);
00067 return iconv_substr($preparedString, 0, $length, self::ICONV_CHARSET) . $etc;
00068 }
00069
00070 return $string;
00071 }
00072
00073
00074
00075
00076
00077
00078
00079 public function strlen($str)
00080 {
00081 return iconv_strlen($str, self::ICONV_CHARSET);
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 public function substr($str, $offset, $length = null)
00093 {
00094 if (is_null($length)) {
00095 $length = iconv_strlen($str, self::ICONV_CHARSET) - $offset;
00096 }
00097 return iconv_substr($str, $offset, $length, self::ICONV_CHARSET);
00098 }
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 public function splitInjection($str, $length = 50, $needle = '-', $insert = ' ')
00110 {
00111 $str = $this->str_split($str, $length);
00112 $newStr = '';
00113 foreach ($str as $part) {
00114 if ($this->strlen($part) >= $length) {
00115 $lastDelimetr = iconv_strpos(strrev($part), $needle, null, self::ICONV_CHARSET);
00116 $tmpNewStr = '';
00117 $tmpNewStr = $this->substr(strrev($part), 0, $lastDelimetr) . $insert . $this->substr(strrev($part), $lastDelimetr);
00118 $newStr .= strrev($tmpNewStr);
00119 } else {
00120 $newStr .= $part;
00121 }
00122 }
00123 return $newStr;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132 public function strrev($str)
00133 {
00134 $result = '';
00135 $strlen = $this->strlen($str);
00136 if (!$strlen) {
00137 return $result;
00138 }
00139 for ($i = $strlen-1; $i >= 0; $i--) {
00140 $result .= iconv_substr($str, $i, 1, self::ICONV_CHARSET);
00141 }
00142 return $result;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 public function str_split($str, $length = 1, $keepWords = false, $trim = false, $wordSeparatorRegex = '\s')
00159 {
00160 $result = array();
00161 $strlen = $this->strlen($str);
00162 if ((!$strlen) || (!is_int($length)) || ($length <= 0)) {
00163 return $result;
00164 }
00165
00166 if ($trim) {
00167 $str = trim(preg_replace('/\s{2,}/is', ' ', $str));
00168 }
00169
00170 if ((!$keepWords) || ($length < 2)) {
00171 for ($offset = 0; $offset < $strlen; $offset += $length) {
00172 $result[] = iconv_substr($str, $offset, $length, self::ICONV_CHARSET);
00173 }
00174 }
00175
00176 else {
00177 $split = preg_split('/(' . $wordSeparatorRegex . '+)/is', $str, null, PREG_SPLIT_DELIM_CAPTURE);
00178 $i = 0;
00179 $space = '';
00180 $spaceLen = 0;
00181 foreach ($split as $key => $part) {
00182 if ($trim) {
00183
00184 if ($key % 2) {
00185 continue;
00186 }
00187 $space = ' ';
00188 $spaceLen = 1;
00189 }
00190 if (empty($result[$i])) {
00191 $currentLength = 0;
00192 $result[$i] = '';
00193 $space = '';
00194 $spaceLen = 0;
00195 }
00196 else {
00197 $currentLength = iconv_strlen($result[$i], self::ICONV_CHARSET);
00198 }
00199 $partLength = iconv_strlen($part, self::ICONV_CHARSET);
00200
00201 if (($currentLength + $spaceLen + $partLength) <= $length) {
00202 $result[$i] .= $space . $part;
00203 }
00204
00205 elseif ($partLength <= $length) {
00206 $i++;
00207 $result[$i] = $part;
00208 }
00209
00210 else {
00211 foreach ($this->str_split($part, $length, false, $trim, $wordSeparatorRegex) as $subpart) {
00212 $i++;
00213 $result[$i] = $subpart;
00214 }
00215 }
00216 }
00217 }
00218
00219 if ($count = count($result)) {
00220 if (empty($result[$count - 1])) {
00221 unset($result[$count - 1]);
00222 }
00223 }
00224
00225 if (isset($result[0]) && empty($result[0])) {
00226 array_shift($result);
00227 }
00228 return $result;
00229 }
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 function splitWords($str, $uniqueOnly = false, $maxWordLenght = 0, $wordSeparatorRegexp = '\s')
00241 {
00242 $result = array();
00243 $split = preg_split('#' . $wordSeparatorRegexp . '#si', $str, null, PREG_SPLIT_NO_EMPTY);
00244 foreach ($split as $key => $word) {
00245 if ($uniqueOnly) {
00246 $result[$word] = $word;
00247 }
00248 else {
00249 $result[] = $word;
00250 }
00251 }
00252 if ($maxWordLenght && count($result) > $maxWordLenght) {
00253 $result = array_slice($result, 0, $maxWordLenght);
00254 }
00255 return $result;
00256 }
00257 }