PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/code/web/public_php/webtt/cake/libs/view/helpers/text.php

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