TextHelper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /**
  3. * Text Helper
  4. *
  5. * Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package Cake.View.Helper
  18. * @since CakePHP(tm) v 0.10.0.1076
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. /**
  22. * Included libraries.
  23. *
  24. */
  25. App::uses('AppHelper', 'View/Helper');
  26. App::uses('HtmlHelper', 'Helper');
  27. App::uses('Multibyte', 'I18n');
  28. /**
  29. * Text helper library.
  30. *
  31. * Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
  32. *
  33. * @package Cake.View.Helper
  34. * @link http://book.cakephp.org/view/1469/Text
  35. */
  36. class TextHelper extends AppHelper {
  37. /**
  38. * helpers
  39. *
  40. * @var array
  41. */
  42. public $helpers = array('Html');
  43. /**
  44. * Highlights a given phrase in a text. You can specify any expression in highlighter that
  45. * may include the \1 expression to include the $phrase found.
  46. *
  47. * ### Options:
  48. *
  49. * - `format` The piece of html with that the phrase will be highlighted
  50. * - `html` If true, will ignore any HTML tags, ensuring that only the correct text is highlighted
  51. *
  52. * @param string $text Text to search the phrase in
  53. * @param string $phrase The phrase that will be searched
  54. * @param array $options An array of html attributes and options.
  55. * @return string The highlighted text
  56. * @access public
  57. * @link http://book.cakephp.org/view/1469/Text#highlight-1622
  58. */
  59. public function highlight($text, $phrase, $options = array()) {
  60. if (empty($phrase)) {
  61. return $text;
  62. }
  63. $default = array(
  64. 'format' => '<span class="highlight">\1</span>',
  65. 'html' => false
  66. );
  67. $options = array_merge($default, $options);
  68. extract($options);
  69. if (is_array($phrase)) {
  70. $replace = array();
  71. $with = array();
  72. foreach ($phrase as $key => $segment) {
  73. $segment = "($segment)";
  74. if ($html) {
  75. $segment = "(?![^<]+>)$segment(?![^<]+>)";
  76. }
  77. $with[] = (is_array($format)) ? $format[$key] : $format;
  78. $replace[] = "|$segment|iu";
  79. }
  80. return preg_replace($replace, $with, $text);
  81. } else {
  82. $phrase = "($phrase)";
  83. if ($html) {
  84. $phrase = "(?![^<]+>)$phrase(?![^<]+>)";
  85. }
  86. return preg_replace("|$phrase|iu", $format, $text);
  87. }
  88. }
  89. /**
  90. * Strips given text of all links (<a href=....)
  91. *
  92. * @param string $text Text
  93. * @return string The text without links
  94. * @access public
  95. * @link http://book.cakephp.org/view/1469/Text#stripLinks-1623
  96. */
  97. public function stripLinks($text) {
  98. return preg_replace('|<a\s+[^>]+>|im', '', preg_replace('|<\/a>|im', '', $text));
  99. }
  100. /**
  101. * Adds links (<a href=....) to a given text, by finding text that begins with
  102. * strings like http:// and ftp://.
  103. *
  104. * @param string $text Text to add links to
  105. * @param array $htmlOptions Array of HTML options.
  106. * @return string The text with links
  107. * @link http://book.cakephp.org/view/1469/Text#autoLinkUrls-1619
  108. */
  109. public function autoLinkUrls($text, $htmlOptions = array()) {
  110. $this->_linkOptions = $htmlOptions;
  111. $text = preg_replace_callback(
  112. '#(?<!href="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i',
  113. array(&$this, '_linkBareUrl'),
  114. $text
  115. );
  116. return preg_replace_callback(
  117. '#(?<!href="|">)(?<!http://|https://|ftp://|nntp://)(www\.[^\n\%\ <]+[^<\n\%\,\.\ <])(?<!\))#i',
  118. array(&$this, '_linkUrls'),
  119. $text
  120. );
  121. }
  122. /**
  123. * Links urls that include http://
  124. *
  125. * @param array $matches
  126. * @return string
  127. * @see TextHelper::autoLinkUrls()
  128. */
  129. private function _linkBareUrl($matches) {
  130. return $this->Html->link($matches[0], $matches[0], $this->_linkOptions);
  131. }
  132. /**
  133. * Links urls missing http://
  134. *
  135. * @param array $matches
  136. * @return string
  137. * @see TextHelper::autoLinkUrls()
  138. */
  139. private function _linkUrls($matches) {
  140. return $this->Html->link($matches[0], 'http://' . $matches[0], $this->_linkOptions);
  141. }
  142. /**
  143. * Links email addresses
  144. *
  145. * @param array $matches
  146. * @return string
  147. * @see TextHelper::autoLinkUrls()
  148. */
  149. private function _linkEmails($matches) {
  150. return $this->Html->link($matches[0], 'mailto:' . $matches[0], $this->_linkOptions);
  151. }
  152. /**
  153. * Adds email links (<a href="mailto:....) to a given text.
  154. *
  155. * @param string $text Text
  156. * @param array $options Array of HTML options.
  157. * @return string The text with links
  158. * @access public
  159. * @link http://book.cakephp.org/view/1469/Text#autoLinkEmails-1618
  160. */
  161. public function autoLinkEmails($text, $options = array()) {
  162. $this->_linkOptions = $options;
  163. $atom = '[a-z0-9!#$%&\'*+\/=?^_`{|}~-]';
  164. return preg_replace_callback(
  165. '/(' . $atom . '+(?:\.' . $atom . '+)*@[a-z0-9-]+(?:\.[a-z0-9-]+)+)/i',
  166. array(&$this, '_linkEmails'),
  167. $text
  168. );
  169. }
  170. /**
  171. * Convert all links and email adresses to HTML links.
  172. *
  173. * @param string $text Text
  174. * @param array $options Array of HTML options.
  175. * @return string The text with links
  176. * @access public
  177. * @link http://book.cakephp.org/view/1469/Text#autoLink-1620
  178. */
  179. public function autoLink($text, $options = array()) {
  180. return $this->autoLinkEmails($this->autoLinkUrls($text, $options), $options);
  181. }
  182. /**
  183. * Truncates text.
  184. *
  185. * Cuts a string to the length of $length and replaces the last characters
  186. * with the ending if the text is longer than length.
  187. *
  188. * ### Options:
  189. *
  190. * - `ending` Will be used as Ending and appended to the trimmed string
  191. * - `exact` If false, $text will not be cut mid-word
  192. * - `html` If true, HTML tags would be handled correctly
  193. *
  194. * @param string $text String to truncate.
  195. * @param integer $length Length of returned string, including ellipsis.
  196. * @param array $options An array of html attributes and options.
  197. * @return string Trimmed string.
  198. * @link http://book.cakephp.org/view/1469/Text#truncate-1625
  199. */
  200. public function truncate($text, $length = 100, $options = array()) {
  201. $default = array(
  202. 'ending' => '...', 'exact' => true, 'html' => false
  203. );
  204. $options = array_merge($default, $options);
  205. extract($options);
  206. if (!function_exists('mb_strlen')) {
  207. class_exists('Multibyte');
  208. }
  209. if ($html) {
  210. if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
  211. return $text;
  212. }
  213. $totalLength = mb_strlen(strip_tags($ending));
  214. $openTags = array();
  215. $truncate = '';
  216. preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
  217. foreach ($tags as $tag) {
  218. if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) {
  219. if (preg_match('/<[\w]+[^>]*>/s', $tag[0])) {
  220. array_unshift($openTags, $tag[2]);
  221. } else if (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) {
  222. $pos = array_search($closeTag[1], $openTags);
  223. if ($pos !== false) {
  224. array_splice($openTags, $pos, 1);
  225. }
  226. }
  227. }
  228. $truncate .= $tag[1];
  229. $contentLength = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3]));
  230. if ($contentLength + $totalLength > $length) {
  231. $left = $length - $totalLength;
  232. $entitiesLength = 0;
  233. 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)) {
  234. foreach ($entities[0] as $entity) {
  235. if ($entity[1] + 1 - $entitiesLength <= $left) {
  236. $left--;
  237. $entitiesLength += mb_strlen($entity[0]);
  238. } else {
  239. break;
  240. }
  241. }
  242. }
  243. $truncate .= mb_substr($tag[3], 0 , $left + $entitiesLength);
  244. break;
  245. } else {
  246. $truncate .= $tag[3];
  247. $totalLength += $contentLength;
  248. }
  249. if ($totalLength >= $length) {
  250. break;
  251. }
  252. }
  253. } else {
  254. if (mb_strlen($text) <= $length) {
  255. return $text;
  256. } else {
  257. $truncate = mb_substr($text, 0, $length - mb_strlen($ending));
  258. }
  259. }
  260. if (!$exact) {
  261. $spacepos = mb_strrpos($truncate, ' ');
  262. if (isset($spacepos)) {
  263. if ($html) {
  264. $bits = mb_substr($truncate, $spacepos);
  265. preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER);
  266. if (!empty($droppedTags)) {
  267. foreach ($droppedTags as $closingTag) {
  268. if (!in_array($closingTag[1], $openTags)) {
  269. array_unshift($openTags, $closingTag[1]);
  270. }
  271. }
  272. }
  273. }
  274. $truncate = mb_substr($truncate, 0, $spacepos);
  275. }
  276. }
  277. $truncate .= $ending;
  278. if ($html) {
  279. foreach ($openTags as $tag) {
  280. $truncate .= '</'.$tag.'>';
  281. }
  282. }
  283. return $truncate;
  284. }
  285. /**
  286. * Extracts an excerpt from the text surrounding the phrase with a number of characters on each side
  287. * determined by radius.
  288. *
  289. * @param string $text String to search the phrase in
  290. * @param string $phrase Phrase that will be searched for
  291. * @param integer $radius The amount of characters that will be returned on each side of the founded phrase
  292. * @param string $ending Ending that will be appended
  293. * @return string Modified string
  294. * @access public
  295. * @link http://book.cakephp.org/view/1469/Text#excerpt-1621
  296. */
  297. public function excerpt($text, $phrase, $radius = 100, $ending = '...') {
  298. if (empty($text) or empty($phrase)) {
  299. return $this->truncate($text, $radius * 2, array('ending' => $ending));
  300. }
  301. $phraseLen = mb_strlen($phrase);
  302. if ($radius < $phraseLen) {
  303. $radius = $phraseLen;
  304. }
  305. $pos = mb_strpos(mb_strtolower($text), mb_strtolower($phrase));
  306. $startPos = 0;
  307. if ($pos > $radius) {
  308. $startPos = $pos - $radius;
  309. }
  310. $textLen = mb_strlen($text);
  311. $endPos = $pos + $phraseLen + $radius;
  312. if ($endPos >= $textLen) {
  313. $endPos = $textLen;
  314. }
  315. $excerpt = mb_substr($text, $startPos, $endPos - $startPos);
  316. if ($startPos != 0) {
  317. $excerpt = substr_replace($excerpt, $ending, 0, $phraseLen);
  318. }
  319. if ($endPos != $textLen) {
  320. $excerpt = substr_replace($excerpt, $ending, -$phraseLen);
  321. }
  322. return $excerpt;
  323. }
  324. /**
  325. * Creates a comma separated list where the last two items are joined with 'and', forming natural English
  326. *
  327. * @param array $list The list to be joined
  328. * @param string $and The word used to join the last and second last items together with. Defaults to 'and'
  329. * @param string $separator The separator used to join all othe other items together. Defaults to ', '
  330. * @return string The glued together string.
  331. * @access public
  332. * @link http://book.cakephp.org/view/1469/Text#toList-1624
  333. */
  334. public function toList($list, $and = 'and', $separator = ', ') {
  335. if (count($list) > 1) {
  336. return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list);
  337. } else {
  338. return array_pop($list);
  339. }
  340. }
  341. }