PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/sunil_nextbits/magento2
PHP | 306 lines | 170 code | 15 blank | 121 comment | 34 complexity | df2ac32847855147b20f1133b21f6682 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) 2012 X.commerce, 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)
  112. . $insert . $this->substr($this->strrev($part), $lastDelimetr);
  113. $newStr .= $this->strrev($tmpNewStr);
  114. } else {
  115. $newStr .= $part;
  116. }
  117. }
  118. return $newStr;
  119. }
  120. /**
  121. * Binary-safe strrev()
  122. *
  123. * @param string $str
  124. * @return string
  125. */
  126. public function strrev($str)
  127. {
  128. $result = '';
  129. $strlen = $this->strlen($str);
  130. if (!$strlen) {
  131. return $result;
  132. }
  133. for ($i = $strlen-1; $i >= 0; $i--) {
  134. $result .= $this->substr($str, $i, 1);
  135. }
  136. return $result;
  137. }
  138. /**
  139. * Binary-safe variant of str_split()
  140. * + option not to break words
  141. * + option to trim spaces (between each word)
  142. * + option to set character(s) (pcre pattern) to be considered as words separator
  143. *
  144. * @param string $str
  145. * @param int $length
  146. * @param bool $keepWords
  147. * @param bool $trim
  148. * @param string $wordSeparatorRegex
  149. * @return array
  150. */
  151. public function str_split($str, $length = 1, $keepWords = false, $trim = false, $wordSeparatorRegex = '\s')
  152. {
  153. $result = array();
  154. $strlen = $this->strlen($str);
  155. if ((!$strlen) || (!is_int($length)) || ($length <= 0)) {
  156. return $result;
  157. }
  158. // trim
  159. if ($trim) {
  160. $str = trim(preg_replace('/\s{2,}/siu', ' ', $str));
  161. }
  162. // do a usual str_split, but safe for our encoding
  163. if ((!$keepWords) || ($length < 2)) {
  164. for ($offset = 0; $offset < $strlen; $offset += $length) {
  165. $result[] = $this->substr($str, $offset, $length);
  166. }
  167. }
  168. // split smartly, keeping words
  169. else {
  170. $split = preg_split('/(' . $wordSeparatorRegex . '+)/siu', $str, null, PREG_SPLIT_DELIM_CAPTURE);
  171. $i = 0;
  172. $space = '';
  173. $spaceLen = 0;
  174. foreach ($split as $key => $part) {
  175. if ($trim) {
  176. // ignore spaces (even keys)
  177. if ($key % 2) {
  178. continue;
  179. }
  180. $space = ' ';
  181. $spaceLen = 1;
  182. }
  183. if (empty($result[$i])) {
  184. $currentLength = 0;
  185. $result[$i] = '';
  186. $space = '';
  187. $spaceLen = 0;
  188. }
  189. else {
  190. $currentLength = $this->strlen($result[$i]);
  191. }
  192. $partLength = $this->strlen($part);
  193. // add part to current last element
  194. if (($currentLength + $spaceLen + $partLength) <= $length) {
  195. $result[$i] .= $space . $part;
  196. }
  197. // add part to new element
  198. elseif ($partLength <= $length) {
  199. $i++;
  200. $result[$i] = $part;
  201. }
  202. // break too long part recursively
  203. else {
  204. foreach ($this->str_split($part, $length, false, $trim, $wordSeparatorRegex) as $subpart) {
  205. $i++;
  206. $result[$i] = $subpart;
  207. }
  208. }
  209. }
  210. }
  211. // remove last element, if empty
  212. if ($count = count($result)) {
  213. if ($result[$count - 1] === '') {
  214. unset($result[$count - 1]);
  215. }
  216. }
  217. // remove first element, if empty
  218. if (isset($result[0]) && $result[0] === '') {
  219. array_shift($result);
  220. }
  221. return $result;
  222. }
  223. /**
  224. * Split words
  225. *
  226. * @param string $str The source string
  227. * @param bool $uniqueOnly Unique words only
  228. * @param int $maxWordLength Limit words count
  229. * @param string $wordSeparatorRegexp
  230. * @return array
  231. */
  232. function splitWords($str, $uniqueOnly = false, $maxWordLength = 0, $wordSeparatorRegexp = '\s')
  233. {
  234. $result = array();
  235. $split = preg_split('#' . $wordSeparatorRegexp . '#siu', $str, null, PREG_SPLIT_NO_EMPTY);
  236. foreach ($split as $word) {
  237. if ($uniqueOnly) {
  238. $result[$word] = $word;
  239. }
  240. else {
  241. $result[] = $word;
  242. }
  243. }
  244. if ($maxWordLength && count($result) > $maxWordLength) {
  245. $result = array_slice($result, 0, $maxWordLength);
  246. }
  247. return $result;
  248. }
  249. /**
  250. * Clean non UTF-8 characters
  251. *
  252. * @param string $string
  253. * @return string
  254. */
  255. public function cleanString($string)
  256. {
  257. return '"libiconv"' == ICONV_IMPL ?
  258. iconv(self::ICONV_CHARSET, self::ICONV_CHARSET . '//IGNORE', $string) : $string;
  259. }
  260. /**
  261. * Find position of first occurrence of a string
  262. *
  263. * @param string $haystack
  264. * @param string $needle
  265. * @param int $offset
  266. * @return int|false
  267. */
  268. public function strpos($haystack, $needle, $offset = null)
  269. {
  270. return iconv_strpos($haystack, $needle, $offset, self::ICONV_CHARSET);
  271. }
  272. /**
  273. * Sorts array with multibyte string keys
  274. *
  275. * @param array $sort
  276. * @return array
  277. */
  278. public function ksortMultibyte(array &$sort)
  279. {
  280. if (empty($sort)) {
  281. return false;
  282. }
  283. $oldLocale = setlocale(LC_COLLATE, "0");
  284. $localeCode = Mage::app()->getLocale()->getLocaleCode();
  285. // use fallback locale if $localeCode is not available
  286. setlocale(LC_COLLATE, $localeCode . '.UTF8', 'C.UTF-8', 'en_US.utf8');
  287. ksort($sort, SORT_LOCALE_STRING);
  288. setlocale(LC_COLLATE, $oldLocale);
  289. return $sort;
  290. }
  291. }