String.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. <?php
  2. /**
  3. * String handling methods.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @since CakePHP(tm) v 1.2.0.5551
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. */
  19. namespace Cake\Utility;
  20. use Cake\Core\Configure;
  21. /**
  22. * String handling methods.
  23. *
  24. *
  25. */
  26. class String {
  27. /**
  28. * Generate a random UUID
  29. *
  30. * @see http://www.ietf.org/rfc/rfc4122.txt
  31. * @return RFC 4122 UUID
  32. */
  33. public static function uuid() {
  34. $node = env('SERVER_ADDR');
  35. if (strpos($node, ':') !== false) {
  36. if (substr_count($node, '::')) {
  37. $node = str_replace(
  38. '::', str_repeat(':0000', 8 - substr_count($node, ':')) . ':', $node
  39. );
  40. }
  41. $node = explode(':', $node);
  42. $ipSix = '';
  43. foreach ($node as $id) {
  44. $ipSix .= str_pad(base_convert($id, 16, 2), 16, 0, STR_PAD_LEFT);
  45. }
  46. $node = base_convert($ipSix, 2, 10);
  47. if (strlen($node) < 38) {
  48. $node = null;
  49. } else {
  50. $node = crc32($node);
  51. }
  52. } elseif (empty($node)) {
  53. $host = env('HOSTNAME');
  54. if (empty($host)) {
  55. $host = env('HOST');
  56. }
  57. if (!empty($host)) {
  58. $ip = gethostbyname($host);
  59. if ($ip === $host) {
  60. $node = crc32($host);
  61. } else {
  62. $node = ip2long($ip);
  63. }
  64. }
  65. } elseif ($node !== '127.0.0.1') {
  66. $node = ip2long($node);
  67. } else {
  68. $node = null;
  69. }
  70. if (empty($node)) {
  71. $node = crc32(Configure::read('Security.salt'));
  72. }
  73. if (function_exists('hphp_get_thread_id')) {
  74. $pid = hphp_get_thread_id();
  75. } elseif (function_exists('zend_thread_id')) {
  76. $pid = zend_thread_id();
  77. } else {
  78. $pid = getmypid();
  79. }
  80. if (!$pid || $pid > 65535) {
  81. $pid = mt_rand(0, 0xfff) | 0x4000;
  82. }
  83. list($timeMid, $timeLow) = explode(' ', microtime());
  84. return sprintf(
  85. "%08x-%04x-%04x-%02x%02x-%04x%08x", (int)$timeLow, (int)substr($timeMid, 2) & 0xffff,
  86. mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3f) | 0x80, mt_rand(0, 0xff), $pid, $node
  87. );
  88. }
  89. /**
  90. * Tokenizes a string using $separator, ignoring any instance of $separator that appears between
  91. * $leftBound and $rightBound
  92. *
  93. * @param string $data The data to tokenize
  94. * @param string $separator The token to split the data on.
  95. * @param string $leftBound The left boundary to ignore separators in.
  96. * @param string $rightBound The right boundary to ignore separators in.
  97. * @return array Array of tokens in $data.
  98. */
  99. public static function tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')') {
  100. if (empty($data) || is_array($data)) {
  101. return $data;
  102. }
  103. $depth = 0;
  104. $offset = 0;
  105. $buffer = '';
  106. $results = array();
  107. $length = strlen($data);
  108. $open = false;
  109. while ($offset <= $length) {
  110. $tmpOffset = -1;
  111. $offsets = array(
  112. strpos($data, $separator, $offset),
  113. strpos($data, $leftBound, $offset),
  114. strpos($data, $rightBound, $offset)
  115. );
  116. for ($i = 0; $i < 3; $i++) {
  117. if ($offsets[$i] !== false && ($offsets[$i] < $tmpOffset || $tmpOffset == -1)) {
  118. $tmpOffset = $offsets[$i];
  119. }
  120. }
  121. if ($tmpOffset !== -1) {
  122. $buffer .= substr($data, $offset, ($tmpOffset - $offset));
  123. if (!$depth && $data{$tmpOffset} == $separator) {
  124. $results[] = $buffer;
  125. $buffer = '';
  126. } else {
  127. $buffer .= $data{$tmpOffset};
  128. }
  129. if ($leftBound != $rightBound) {
  130. if ($data{$tmpOffset} == $leftBound) {
  131. $depth++;
  132. }
  133. if ($data{$tmpOffset} == $rightBound) {
  134. $depth--;
  135. }
  136. } else {
  137. if ($data{$tmpOffset} == $leftBound) {
  138. if (!$open) {
  139. $depth++;
  140. $open = true;
  141. } else {
  142. $depth--;
  143. }
  144. }
  145. }
  146. $offset = ++$tmpOffset;
  147. } else {
  148. $results[] = $buffer . substr($data, $offset);
  149. $offset = $length + 1;
  150. }
  151. }
  152. if (empty($results) && !empty($buffer)) {
  153. $results[] = $buffer;
  154. }
  155. if (!empty($results)) {
  156. return array_map('trim', $results);
  157. }
  158. return array();
  159. }
  160. /**
  161. * Replaces variable placeholders inside a $str with any given $data. Each key in the $data array
  162. * corresponds to a variable placeholder name in $str.
  163. * Example: `String::insert(':name is :age years old.', array('name' => 'Bob', '65'));`
  164. * Returns: Bob is 65 years old.
  165. *
  166. * Available $options are:
  167. *
  168. * - before: The character or string in front of the name of the variable placeholder (Defaults to `:`)
  169. * - after: The character or string after the name of the variable placeholder (Defaults to null)
  170. * - escape: The character or string used to escape the before character / string (Defaults to `\`)
  171. * - format: A regex to use for matching variable placeholders. Default is: `/(?<!\\)\:%s/`
  172. * (Overwrites before, after, breaks escape / clean)
  173. * - clean: A boolean or array with instructions for String::cleanInsert
  174. *
  175. * @param string $str A string containing variable placeholders
  176. * @param array $data A key => val array where each key stands for a placeholder variable name
  177. * to be replaced with val
  178. * @param array $options An array of options, see description above
  179. * @return string
  180. */
  181. public static function insert($str, $data, $options = array()) {
  182. $defaults = array(
  183. 'before' => ':', 'after' => null, 'escape' => '\\', 'format' => null, 'clean' => false
  184. );
  185. $options += $defaults;
  186. $format = $options['format'];
  187. $data = (array)$data;
  188. if (empty($data)) {
  189. return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
  190. }
  191. if (!isset($format)) {
  192. $format = sprintf(
  193. '/(?<!%s)%s%%s%s/',
  194. preg_quote($options['escape'], '/'),
  195. str_replace('%', '%%', preg_quote($options['before'], '/')),
  196. str_replace('%', '%%', preg_quote($options['after'], '/'))
  197. );
  198. }
  199. if (strpos($str, '?') !== false && is_numeric(key($data))) {
  200. $offset = 0;
  201. while (($pos = strpos($str, '?', $offset)) !== false) {
  202. $val = array_shift($data);
  203. $offset = $pos + strlen($val);
  204. $str = substr_replace($str, $val, $pos, 1);
  205. }
  206. return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
  207. }
  208. asort($data);
  209. $dataKeys = array_keys($data);
  210. $hashKeys = array_map('crc32', $dataKeys);
  211. $tempData = array_combine($dataKeys, $hashKeys);
  212. krsort($tempData);
  213. foreach ($tempData as $key => $hashVal) {
  214. $key = sprintf($format, preg_quote($key, '/'));
  215. $str = preg_replace($key, $hashVal, $str);
  216. }
  217. $dataReplacements = array_combine($hashKeys, array_values($data));
  218. foreach ($dataReplacements as $tmpHash => $tmpValue) {
  219. $tmpValue = (is_array($tmpValue)) ? '' : $tmpValue;
  220. $str = str_replace($tmpHash, $tmpValue, $str);
  221. }
  222. if (!isset($options['format']) && isset($options['before'])) {
  223. $str = str_replace($options['escape'] . $options['before'], $options['before'], $str);
  224. }
  225. return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
  226. }
  227. /**
  228. * Cleans up a String::insert() formatted string with given $options depending on the 'clean' key in
  229. * $options. The default method used is text but html is also available. The goal of this function
  230. * is to replace all whitespace and unneeded markup around placeholders that did not get replaced
  231. * by String::insert().
  232. *
  233. * @param string $str
  234. * @param array $options
  235. * @return string
  236. * @see String::insert()
  237. */
  238. public static function cleanInsert($str, $options) {
  239. $clean = $options['clean'];
  240. if (!$clean) {
  241. return $str;
  242. }
  243. if ($clean === true) {
  244. $clean = array('method' => 'text');
  245. }
  246. if (!is_array($clean)) {
  247. $clean = array('method' => $options['clean']);
  248. }
  249. switch ($clean['method']) {
  250. case 'html':
  251. $clean = array_merge(array(
  252. 'word' => '[\w,.]+',
  253. 'andText' => true,
  254. 'replacement' => '',
  255. ), $clean);
  256. $kleenex = sprintf(
  257. '/[\s]*[a-z]+=(")(%s%s%s[\s]*)+\\1/i',
  258. preg_quote($options['before'], '/'),
  259. $clean['word'],
  260. preg_quote($options['after'], '/')
  261. );
  262. $str = preg_replace($kleenex, $clean['replacement'], $str);
  263. if ($clean['andText']) {
  264. $options['clean'] = array('method' => 'text');
  265. $str = String::cleanInsert($str, $options);
  266. }
  267. break;
  268. case 'text':
  269. $clean = array_merge(array(
  270. 'word' => '[\w,.]+',
  271. 'gap' => '[\s]*(?:(?:and|or)[\s]*)?',
  272. 'replacement' => '',
  273. ), $clean);
  274. $kleenex = sprintf(
  275. '/(%s%s%s%s|%s%s%s%s)/',
  276. preg_quote($options['before'], '/'),
  277. $clean['word'],
  278. preg_quote($options['after'], '/'),
  279. $clean['gap'],
  280. $clean['gap'],
  281. preg_quote($options['before'], '/'),
  282. $clean['word'],
  283. preg_quote($options['after'], '/')
  284. );
  285. $str = preg_replace($kleenex, $clean['replacement'], $str);
  286. break;
  287. }
  288. return $str;
  289. }
  290. /**
  291. * Wraps text to a specific width, can optionally wrap at word breaks.
  292. *
  293. * ### Options
  294. *
  295. * - `width` The width to wrap to. Defaults to 72.
  296. * - `wordWrap` Only wrap on words breaks (spaces) Defaults to true.
  297. * - `indent` String to indent with. Defaults to null.
  298. * - `indentAt` 0 based index to start indenting at. Defaults to 0.
  299. *
  300. * @param string $text The text to format.
  301. * @param array|integer $options Array of options to use, or an integer to wrap the text to.
  302. * @return string Formatted text.
  303. */
  304. public static function wrap($text, $options = array()) {
  305. if (is_numeric($options)) {
  306. $options = array('width' => $options);
  307. }
  308. $options += array('width' => 72, 'wordWrap' => true, 'indent' => null, 'indentAt' => 0);
  309. if ($options['wordWrap']) {
  310. $wrapped = self::wordWrap($text, $options['width'], "\n");
  311. } else {
  312. $wrapped = trim(chunk_split($text, $options['width'] - 1, "\n"));
  313. }
  314. if (!empty($options['indent'])) {
  315. $chunks = explode("\n", $wrapped);
  316. for ($i = $options['indentAt'], $len = count($chunks); $i < $len; $i++) {
  317. $chunks[$i] = $options['indent'] . $chunks[$i];
  318. }
  319. $wrapped = implode("\n", $chunks);
  320. }
  321. return $wrapped;
  322. }
  323. /**
  324. * Unicode aware version of wordwrap.
  325. *
  326. * @param string $text The text to format.
  327. * @param integer $width The width to wrap to. Defaults to 72.
  328. * @param string $break The line is broken using the optional break parameter. Defaults to '\n'.
  329. * @param boolean $cut If the cut is set to true, the string is always wrapped at the specified width.
  330. * @return string Formatted text.
  331. */
  332. public static function wordWrap($text, $width = 72, $break = "\n", $cut = false) {
  333. if ($cut) {
  334. $parts = array();
  335. while (mb_strlen($text) > 0) {
  336. $part = mb_substr($text, 0, $width);
  337. $parts[] = trim($part);
  338. $text = trim(mb_substr($text, mb_strlen($part)));
  339. }
  340. return implode($break, $parts);
  341. }
  342. $parts = array();
  343. while (mb_strlen($text) > 0) {
  344. if ($width >= mb_strlen($text)) {
  345. $parts[] = trim($text);
  346. break;
  347. }
  348. $part = mb_substr($text, 0, $width);
  349. $nextChar = mb_substr($text, $width, 1);
  350. if ($nextChar !== ' ') {
  351. $breakAt = mb_strrpos($part, ' ');
  352. if ($breakAt === false) {
  353. $breakAt = mb_strpos($text, ' ', $width);
  354. }
  355. if ($breakAt === false) {
  356. $parts[] = trim($text);
  357. break;
  358. }
  359. $part = mb_substr($text, 0, $breakAt);
  360. }
  361. $part = trim($part);
  362. $parts[] = $part;
  363. $text = trim(mb_substr($text, mb_strlen($part)));
  364. }
  365. return implode($break, $parts);
  366. }
  367. /**
  368. * Highlights a given phrase in a text. You can specify any expression in highlighter that
  369. * may include the \1 expression to include the $phrase found.
  370. *
  371. * ### Options:
  372. *
  373. * - `format` The piece of html with that the phrase will be highlighted
  374. * - `html` If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
  375. * - `regex` a custom regex rule that is used to match words, default is '|$tag|iu'
  376. *
  377. * @param string $text Text to search the phrase in
  378. * @param string $phrase The phrase that will be searched
  379. * @param array $options An array of html attributes and options.
  380. * @return string The highlighted text
  381. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::highlight
  382. */
  383. public static function highlight($text, $phrase, $options = array()) {
  384. if (empty($phrase)) {
  385. return $text;
  386. }
  387. $default = array(
  388. 'format' => '<span class="highlight">\1</span>',
  389. 'html' => false,
  390. 'regex' => "|%s|iu"
  391. );
  392. $options = array_merge($default, $options);
  393. extract($options);
  394. if (is_array($phrase)) {
  395. $replace = array();
  396. $with = array();
  397. foreach ($phrase as $key => $segment) {
  398. $segment = '(' . preg_quote($segment, '|') . ')';
  399. if ($html) {
  400. $segment = "(?![^<]+>)$segment(?![^<]+>)";
  401. }
  402. $with[] = (is_array($format)) ? $format[$key] : $format;
  403. $replace[] = sprintf($options['regex'], $segment);
  404. }
  405. return preg_replace($replace, $with, $text);
  406. }
  407. $phrase = '(' . preg_quote($phrase, '|') . ')';
  408. if ($html) {
  409. $phrase = "(?![^<]+>)$phrase(?![^<]+>)";
  410. }
  411. return preg_replace(sprintf($options['regex'], $phrase), $format, $text);
  412. }
  413. /**
  414. * Strips given text of all links (<a href=....)
  415. *
  416. * @param string $text Text
  417. * @return string The text without links
  418. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::stripLinks
  419. */
  420. public static function stripLinks($text) {
  421. return preg_replace('|<a\s+[^>]+>|im', '', preg_replace('|<\/a>|im', '', $text));
  422. }
  423. /**
  424. * Truncates text starting from the end.
  425. *
  426. * Cuts a string to the length of $length and replaces the first characters
  427. * with the ellipsis if the text is longer than length.
  428. *
  429. * ### Options:
  430. *
  431. * - `ellipsis` Will be used as Beginning and prepended to the trimmed string
  432. * - `exact` If false, $text will not be cut mid-word
  433. *
  434. * @param string $text String to truncate.
  435. * @param integer $length Length of returned string, including ellipsis.
  436. * @param array $options An array of options.
  437. * @return string Trimmed string.
  438. */
  439. public static function tail($text, $length = 100, $options = array()) {
  440. $default = array(
  441. 'ellipsis' => '...', 'exact' => true
  442. );
  443. $options = array_merge($default, $options);
  444. extract($options);
  445. if (mb_strlen($text) <= $length) {
  446. return $text;
  447. }
  448. $truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($ellipsis));
  449. if (!$exact) {
  450. $spacepos = mb_strpos($truncate, ' ');
  451. $truncate = $spacepos === false ? '' : trim(mb_substr($truncate, $spacepos));
  452. }
  453. return $ellipsis . $truncate;
  454. }
  455. /**
  456. * Truncates text.
  457. *
  458. * Cuts a string to the length of $length and replaces the last characters
  459. * with the ellipsis if the text is longer than length.
  460. *
  461. * ### Options:
  462. *
  463. * - `ellipsis` Will be used as Ending and appended to the trimmed string (`ending` is deprecated)
  464. * - `exact` If false, $text will not be cut mid-word
  465. * - `html` If true, HTML tags would be handled correctly
  466. *
  467. * @param string $text String to truncate.
  468. * @param integer $length Length of returned string, including ellipsis.
  469. * @param array $options An array of html attributes and options.
  470. * @return string Trimmed string.
  471. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::truncate
  472. */
  473. public static function truncate($text, $length = 100, $options = array()) {
  474. $default = array(
  475. 'ellipsis' => '...', 'exact' => true, 'html' => false
  476. );
  477. if (isset($options['ending'])) {
  478. $default['ellipsis'] = $options['ending'];
  479. } elseif (!empty($options['html']) && Configure::read('App.encoding') === 'UTF-8') {
  480. $default['ellipsis'] = "\xe2\x80\xa6";
  481. }
  482. $options = array_merge($default, $options);
  483. extract($options);
  484. if ($html) {
  485. if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
  486. return $text;
  487. }
  488. $totalLength = mb_strlen(strip_tags($ellipsis));
  489. $openTags = array();
  490. $truncate = '';
  491. preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
  492. foreach ($tags as $tag) {
  493. if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) {
  494. if (preg_match('/<[\w]+[^>]*>/s', $tag[0])) {
  495. array_unshift($openTags, $tag[2]);
  496. } elseif (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) {
  497. $pos = array_search($closeTag[1], $openTags);
  498. if ($pos !== false) {
  499. array_splice($openTags, $pos, 1);
  500. }
  501. }
  502. }
  503. $truncate .= $tag[1];
  504. $contentLength = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3]));
  505. if ($contentLength + $totalLength > $length) {
  506. $left = $length - $totalLength;
  507. $entitiesLength = 0;
  508. if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE)) {
  509. foreach ($entities[0] as $entity) {
  510. if ($entity[1] + 1 - $entitiesLength <= $left) {
  511. $left--;
  512. $entitiesLength += mb_strlen($entity[0]);
  513. } else {
  514. break;
  515. }
  516. }
  517. }
  518. $truncate .= mb_substr($tag[3], 0, $left + $entitiesLength);
  519. break;
  520. } else {
  521. $truncate .= $tag[3];
  522. $totalLength += $contentLength;
  523. }
  524. if ($totalLength >= $length) {
  525. break;
  526. }
  527. }
  528. } else {
  529. if (mb_strlen($text) <= $length) {
  530. return $text;
  531. }
  532. $truncate = mb_substr($text, 0, $length - mb_strlen($ellipsis));
  533. }
  534. if (!$exact) {
  535. $spacepos = mb_strrpos($truncate, ' ');
  536. if ($html) {
  537. $truncateCheck = mb_substr($truncate, 0, $spacepos);
  538. $lastOpenTag = mb_strrpos($truncateCheck, '<');
  539. $lastCloseTag = mb_strrpos($truncateCheck, '>');
  540. if ($lastOpenTag > $lastCloseTag) {
  541. preg_match_all('/<[\w]+[^>]*>/s', $truncate, $lastTagMatches);
  542. $lastTag = array_pop($lastTagMatches[0]);
  543. $spacepos = mb_strrpos($truncate, $lastTag) + mb_strlen($lastTag);
  544. }
  545. $bits = mb_substr($truncate, $spacepos);
  546. preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER);
  547. if (!empty($droppedTags)) {
  548. if (!empty($openTags)) {
  549. foreach ($droppedTags as $closingTag) {
  550. if (!in_array($closingTag[1], $openTags)) {
  551. array_unshift($openTags, $closingTag[1]);
  552. }
  553. }
  554. } else {
  555. foreach ($droppedTags as $closingTag) {
  556. $openTags[] = $closingTag[1];
  557. }
  558. }
  559. }
  560. }
  561. $truncate = mb_substr($truncate, 0, $spacepos);
  562. }
  563. $truncate .= $ellipsis;
  564. if ($html) {
  565. foreach ($openTags as $tag) {
  566. $truncate .= '</' . $tag . '>';
  567. }
  568. }
  569. return $truncate;
  570. }
  571. /**
  572. * Extracts an excerpt from the text surrounding the phrase with a number of characters on each side
  573. * determined by radius.
  574. *
  575. * @param string $text String to search the phrase in
  576. * @param string $phrase Phrase that will be searched for
  577. * @param integer $radius The amount of characters that will be returned on each side of the founded phrase
  578. * @param string $ellipsis Ending that will be appended
  579. * @return string Modified string
  580. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::excerpt
  581. */
  582. public static function excerpt($text, $phrase, $radius = 100, $ellipsis = '...') {
  583. if (empty($text) || empty($phrase)) {
  584. return static::truncate($text, $radius * 2, array('ellipsis' => $ellipsis));
  585. }
  586. $append = $prepend = $ellipsis;
  587. $phraseLen = mb_strlen($phrase);
  588. $textLen = mb_strlen($text);
  589. $pos = mb_strpos(mb_strtolower($text), mb_strtolower($phrase));
  590. if ($pos === false) {
  591. return mb_substr($text, 0, $radius) . $ellipsis;
  592. }
  593. $startPos = $pos - $radius;
  594. if ($startPos <= 0) {
  595. $startPos = 0;
  596. $prepend = '';
  597. }
  598. $endPos = $pos + $phraseLen + $radius;
  599. if ($endPos >= $textLen) {
  600. $endPos = $textLen;
  601. $append = '';
  602. }
  603. $excerpt = mb_substr($text, $startPos, $endPos - $startPos);
  604. $excerpt = $prepend . $excerpt . $append;
  605. return $excerpt;
  606. }
  607. /**
  608. * Creates a comma separated list where the last two items are joined with 'and', forming natural English
  609. *
  610. * @param array $list The list to be joined
  611. * @param string $and The word used to join the last and second last items together with. Defaults to 'and'
  612. * @param string $separator The separator used to join all the other items together. Defaults to ', '
  613. * @return string The glued together string.
  614. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::toList
  615. */
  616. public static function toList($list, $and = 'and', $separator = ', ') {
  617. if (count($list) > 1) {
  618. return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list);
  619. }
  620. return array_pop($list);
  621. }
  622. /**
  623. * Check if the string contain multibyte characters
  624. *
  625. * @param string $string value to test
  626. * @return boolean
  627. */
  628. public static function isMultibyte($string) {
  629. $length = strlen($string);
  630. for ($i = 0; $i < $length; $i++ ) {
  631. $value = ord(($string[$i]));
  632. if ($value > 128) {
  633. return true;
  634. }
  635. }
  636. return false;
  637. }
  638. /**
  639. * Converts a multibyte character string
  640. * to the decimal value of the character
  641. *
  642. * @param string $string
  643. * @return array
  644. */
  645. public static function utf8($string) {
  646. $map = array();
  647. $values = array();
  648. $find = 1;
  649. $length = strlen($string);
  650. for ($i = 0; $i < $length; $i++) {
  651. $value = ord($string[$i]);
  652. if ($value < 128) {
  653. $map[] = $value;
  654. } else {
  655. if (empty($values)) {
  656. $find = ($value < 224) ? 2 : 3;
  657. }
  658. $values[] = $value;
  659. if (count($values) === $find) {
  660. if ($find == 3) {
  661. $map[] = (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64);
  662. } else {
  663. $map[] = (($values[0] % 32) * 64) + ($values[1] % 64);
  664. }
  665. $values = array();
  666. $find = 1;
  667. }
  668. }
  669. }
  670. return $map;
  671. }
  672. /**
  673. * Converts the decimal value of a multibyte character string
  674. * to a string
  675. *
  676. * @param array $array
  677. * @return string
  678. */
  679. public static function ascii($array) {
  680. $ascii = '';
  681. foreach ($array as $utf8) {
  682. if ($utf8 < 128) {
  683. $ascii .= chr($utf8);
  684. } elseif ($utf8 < 2048) {
  685. $ascii .= chr(192 + (($utf8 - ($utf8 % 64)) / 64));
  686. $ascii .= chr(128 + ($utf8 % 64));
  687. } else {
  688. $ascii .= chr(224 + (($utf8 - ($utf8 % 4096)) / 4096));
  689. $ascii .= chr(128 + ((($utf8 % 4096) - ($utf8 % 64)) / 64));
  690. $ascii .= chr(128 + ($utf8 % 64));
  691. }
  692. }
  693. return $ascii;
  694. }
  695. }