PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Core/Helper/String.php

https://github.com/rgranadino/magento-mirror
PHP | 302 lines | 167 code | 15 blank | 120 comment | 34 complexity | 726ef22f608e044627832f633efa8575 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Core data helper
  28. *
  29. * @author Magento Core Team <core@magentocommerce.com>
  30. */
  31. class Mage_Core_Helper_String extends Mage_Core_Helper_Abstract
  32. {
  33. const ICONV_CHARSET = 'UTF-8';
  34. /**
  35. * Truncate a string to a certain length if necessary, appending the $etc string.
  36. * $remainder will contain the string that has been replaced with $etc.
  37. *
  38. * @param string $string
  39. * @param int $length
  40. * @param string $etc
  41. * @param string &$remainder
  42. * @param bool $breakWords
  43. * @return string
  44. */
  45. public function truncate($string, $length = 80, $etc = '...', &$remainder = '', $breakWords = true)
  46. {
  47. $remainder = '';
  48. if (0 == $length) {
  49. return '';
  50. }
  51. $originalLength = $this->strlen($string);
  52. if ($originalLength > $length) {
  53. $length -= $this->strlen($etc);
  54. if ($length <= 0) {
  55. return '';
  56. }
  57. $preparedString = $string;
  58. $preparedlength = $length;
  59. if (!$breakWords) {
  60. $preparedString = preg_replace('/\s+?(\S+)?$/u', '', $this->substr($string, 0, $length + 1));
  61. $preparedlength = $this->strlen($preparedString);
  62. }
  63. $remainder = $this->substr($string, $preparedlength, $originalLength);
  64. return $this->substr($preparedString, 0, $length) . $etc;
  65. }
  66. return $string;
  67. }
  68. /**
  69. * Retrieve string length using default charset
  70. *
  71. * @param string $string
  72. * @return int
  73. */
  74. public function strlen($string)
  75. {
  76. return iconv_strlen($string, self::ICONV_CHARSET);
  77. }
  78. /**
  79. * Passthrough to iconv_substr()
  80. *
  81. * @param string $string
  82. * @param int $offset
  83. * @param int $length
  84. * @return string
  85. */
  86. public function substr($string, $offset, $length = null)
  87. {
  88. $string = $this->cleanString($string);
  89. if (is_null($length)) {
  90. $length = $this->strlen($string) - $offset;
  91. }
  92. return iconv_substr($string, $offset, $length, self::ICONV_CHARSET);
  93. }
  94. /**
  95. * Split string and appending $insert string after $needle
  96. *
  97. * @param string $str
  98. * @param integer $length
  99. * @param string $needle
  100. * @param string $insert
  101. * @return string
  102. */
  103. public function splitInjection($str, $length = 50, $needle = '-', $insert = ' ')
  104. {
  105. $str = $this->str_split($str, $length);
  106. $newStr = '';
  107. foreach ($str as $part) {
  108. if ($this->strlen($part) >= $length) {
  109. $lastDelimetr = $this->strpos($this->strrev($part), $needle);
  110. $tmpNewStr = '';
  111. $tmpNewStr = $this->substr($this->strrev($part), 0, $lastDelimetr) . $insert . $this->substr($this->strrev($part), $lastDelimetr);
  112. $newStr .= $this->strrev($tmpNewStr);
  113. } else {
  114. $newStr .= $part;
  115. }
  116. }
  117. return $newStr;
  118. }
  119. /**
  120. * Binary-safe strrev()
  121. *
  122. * @param string $str
  123. * @return string
  124. */
  125. public function strrev($str)
  126. {
  127. $result = '';
  128. $strlen = $this->strlen($str);
  129. if (!$strlen) {
  130. return $result;
  131. }
  132. for ($i = $strlen-1; $i >= 0; $i--) {
  133. $result .= $this->substr($str, $i, 1);
  134. }
  135. return $result;
  136. }
  137. /**
  138. * Binary-safe variant of str_split()
  139. * + option not to break words
  140. * + option to trim spaces (between each word)
  141. * + option to set character(s) (pcre pattern) to be considered as words separator
  142. *
  143. * @param string $str
  144. * @param int $length
  145. * @param bool $keepWords
  146. * @param bool $trim
  147. * @param string $wordSeparatorRegex
  148. * @return array
  149. */
  150. public function str_split($str, $length = 1, $keepWords = false, $trim = false, $wordSeparatorRegex = '\s')
  151. {
  152. $result = array();
  153. $strlen = $this->strlen($str);
  154. if ((!$strlen) || (!is_int($length)) || ($length <= 0)) {
  155. return $result;
  156. }
  157. // trim
  158. if ($trim) {
  159. $str = trim(preg_replace('/\s{2,}/siu', ' ', $str));
  160. }
  161. // do a usual str_split, but safe for our encoding
  162. if ((!$keepWords) || ($length < 2)) {
  163. for ($offset = 0; $offset < $strlen; $offset += $length) {
  164. $result[] = $this->substr($str, $offset, $length);
  165. }
  166. }
  167. // split smartly, keeping words
  168. else {
  169. $split = preg_split('/(' . $wordSeparatorRegex . '+)/siu', $str, null, PREG_SPLIT_DELIM_CAPTURE);
  170. $i = 0;
  171. $space = '';
  172. $spaceLen = 0;
  173. foreach ($split as $key => $part) {
  174. if ($trim) {
  175. // ignore spaces (even keys)
  176. if ($key % 2) {
  177. continue;
  178. }
  179. $space = ' ';
  180. $spaceLen = 1;
  181. }
  182. if (empty($result[$i])) {
  183. $currentLength = 0;
  184. $result[$i] = '';
  185. $space = '';
  186. $spaceLen = 0;
  187. }
  188. else {
  189. $currentLength = $this->strlen($result[$i]);
  190. }
  191. $partLength = $this->strlen($part);
  192. // add part to current last element
  193. if (($currentLength + $spaceLen + $partLength) <= $length) {
  194. $result[$i] .= $space . $part;
  195. }
  196. // add part to new element
  197. elseif ($partLength <= $length) {
  198. $i++;
  199. $result[$i] = $part;
  200. }
  201. // break too long part recursively
  202. else {
  203. foreach ($this->str_split($part, $length, false, $trim, $wordSeparatorRegex) as $subpart) {
  204. $i++;
  205. $result[$i] = $subpart;
  206. }
  207. }
  208. }
  209. }
  210. // remove last element, if empty
  211. if ($count = count($result)) {
  212. if ($result[$count - 1] === '') {
  213. unset($result[$count - 1]);
  214. }
  215. }
  216. // remove first element, if empty
  217. if (isset($result[0]) && $result[0] === '') {
  218. array_shift($result);
  219. }
  220. return $result;
  221. }
  222. /**
  223. * Split words
  224. *
  225. * @param string $str The source string
  226. * @param bool $uniqueOnly Unique words only
  227. * @param int $maxWordLength Limit words count
  228. * @param string $wordSeparatorRegexp
  229. * @return array
  230. */
  231. function splitWords($str, $uniqueOnly = false, $maxWordLength = 0, $wordSeparatorRegexp = '\s')
  232. {
  233. $result = array();
  234. $split = preg_split('#' . $wordSeparatorRegexp . '#siu', $str, null, PREG_SPLIT_NO_EMPTY);
  235. foreach ($split as $word) {
  236. if ($uniqueOnly) {
  237. $result[$word] = $word;
  238. }
  239. else {
  240. $result[] = $word;
  241. }
  242. }
  243. if ($maxWordLength && count($result) > $maxWordLength) {
  244. $result = array_slice($result, 0, $maxWordLength);
  245. }
  246. return $result;
  247. }
  248. /**
  249. * Clean non UTF-8 characters
  250. *
  251. * @param string $string
  252. * @return string
  253. */
  254. public function cleanString($string)
  255. {
  256. return '"libiconv"' == ICONV_IMPL ? iconv(self::ICONV_CHARSET, self::ICONV_CHARSET . '//IGNORE', $string) : $string;
  257. }
  258. /**
  259. * Find position of first occurrence of a string
  260. *
  261. * @param string $haystack
  262. * @param string $needle
  263. * @param int $offset
  264. * @return int|false
  265. */
  266. public function strpos($haystack, $needle, $offset = null)
  267. {
  268. return iconv_strpos($haystack, $needle, $offset, self::ICONV_CHARSET);
  269. }
  270. /**
  271. * Sorts array with multibyte string keys
  272. *
  273. * @param array $sort
  274. * @return array
  275. */
  276. public function ksortMultibyte(array &$sort)
  277. {
  278. if (empty($sort)) {
  279. return false;
  280. }
  281. $oldLocale = setlocale(LC_COLLATE, "0");
  282. setlocale(LC_COLLATE, Mage::app()->getLocale()->getLocaleCode() . '.UTF8');
  283. ksort($sort, SORT_LOCALE_STRING);
  284. setlocale(LC_COLLATE, $oldLocale);
  285. return $sort;
  286. }
  287. }