TextHelper.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. App::uses('AppHelper', 'View/Helper');
  22. /**
  23. * Text helper library.
  24. *
  25. * Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
  26. *
  27. * @package Cake.View.Helper
  28. * @property HtmlHelper $Html
  29. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html
  30. * @see String
  31. */
  32. class TextHelper extends AppHelper {
  33. /**
  34. * helpers
  35. *
  36. * @var array
  37. */
  38. public $helpers = array('Html');
  39. /**
  40. * An array of md5sums and their contents.
  41. * Used when inserting links into text.
  42. *
  43. * @var array
  44. */
  45. protected $_placeholders = array();
  46. /**
  47. * String utility instance
  48. */
  49. protected $_String;
  50. /**
  51. * Constructor
  52. *
  53. * @param View $View the view object the helper is attached to.
  54. * @param array $settings Settings array Settings array
  55. */
  56. public function __construct(View $View, $settings = array()) {
  57. $settings = Set::merge(array('engine' => 'String'), $settings);
  58. parent::__construct($View, $settings);
  59. $engineClass = $settings['engine'];
  60. App::uses($engineClass, 'Utility');
  61. if (class_exists($engineClass)) {
  62. $this->_String = new $engineClass($settings);
  63. } else {
  64. throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
  65. }
  66. }
  67. /**
  68. * Call methods from String utility class
  69. */
  70. public function __call($method, $params) {
  71. return call_user_func_array(array($this->_String, $method), $params);
  72. }
  73. /**
  74. * Adds links (<a href=....) to a given text, by finding text that begins with
  75. * strings like http:// and ftp://.
  76. *
  77. * ### Options
  78. *
  79. * - `escape` Control HTML escaping of input. Defaults to true.
  80. *
  81. * @param string $text Text
  82. * @param array $options Array of HTML options, and options listed above.
  83. * @return string The text with links
  84. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkUrls
  85. */
  86. public function autoLinkUrls($text, $options = array()) {
  87. $this->_placeholders = array();
  88. $options += array('escape' => true);
  89. $text = preg_replace_callback(
  90. '#(?<!href="|src="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i',
  91. array(&$this, '_insertPlaceHolder'),
  92. $text
  93. );
  94. $text = preg_replace_callback(
  95. '#(?<!href="|">)(?<!http://|https://|ftp://|nntp://)(www\.[^\n\%\ <]+[^<\n\%\,\.\ <])(?<!\))#i',
  96. array(&$this, '_insertPlaceHolder'),
  97. $text
  98. );
  99. if ($options['escape']) {
  100. $text = h($text);
  101. }
  102. return $this->_linkUrls($text, $options);
  103. }
  104. /**
  105. * Saves the placeholder for a string, for later use. This gets around double
  106. * escaping content in URL's.
  107. *
  108. * @param array $matches An array of regexp matches.
  109. * @return string Replaced values.
  110. */
  111. protected function _insertPlaceHolder($matches) {
  112. $key = md5($matches[0]);
  113. $this->_placeholders[$key] = $matches[0];
  114. return $key;
  115. }
  116. /**
  117. * Replace placeholders with links.
  118. *
  119. * @param string $text The text to operate on.
  120. * @param array $htmlOptions The options for the generated links.
  121. * @return string The text with links inserted.
  122. */
  123. protected function _linkUrls($text, $htmlOptions) {
  124. $replace = array();
  125. foreach ($this->_placeholders as $md5 => $url) {
  126. $link = $url;
  127. if (!preg_match('#^[a-z]+\://#', $url)) {
  128. $url = 'http://' . $url;
  129. }
  130. $replace[$md5] = $this->Html->link($link, $url, $htmlOptions);
  131. }
  132. return strtr($text, $replace);
  133. }
  134. /**
  135. * Links email addresses
  136. *
  137. * @param string $text The text to operate on
  138. * @param array $options An array of options to use for the HTML.
  139. * @return string
  140. * @see TextHelper::autoLinkEmails()
  141. */
  142. protected function _linkEmails($text, $options) {
  143. $replace = array();
  144. foreach ($this->_placeholders as $md5 => $url) {
  145. $replace[$md5] = $this->Html->link($url, 'mailto:' . $url, $options);
  146. }
  147. return strtr($text, $replace);
  148. }
  149. /**
  150. * Adds email links (<a href="mailto:....) to a given text.
  151. *
  152. * ### Options
  153. *
  154. * - `escape` Control HTML escaping of input. Defaults to true.
  155. *
  156. * @param string $text Text
  157. * @param array $options Array of HTML options, and options listed above.
  158. * @return string The text with links
  159. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkEmails
  160. */
  161. public function autoLinkEmails($text, $options = array()) {
  162. $options += array('escape' => true);
  163. $this->_placeholders = array();
  164. $atom = '[a-z0-9!#$%&\'*+\/=?^_`{|}~-]';
  165. $text = preg_replace_callback(
  166. '/(' . $atom . '+(?:\.' . $atom . '+)*@[a-z0-9-]+(?:\.[a-z0-9-]+)+)/i',
  167. array(&$this, '_insertPlaceholder'),
  168. $text
  169. );
  170. if ($options['escape']) {
  171. $text = h($text);
  172. }
  173. return $this->_linkEmails($text, $options);
  174. }
  175. /**
  176. * Convert all links and email addresses to HTML links.
  177. *
  178. * ### Options
  179. *
  180. * - `escape` Control HTML escaping of input. Defaults to true.
  181. *
  182. * @param string $text Text
  183. * @param array $options Array of HTML options, and options listed above.
  184. * @return string The text with links
  185. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLink
  186. */
  187. public function autoLink($text, $options = array()) {
  188. $text = $this->autoLinkUrls($text, $options);
  189. return $this->autoLinkEmails($text, array_merge($options, array('escape' => false)));
  190. }
  191. /**
  192. * @see String::highlight()
  193. *
  194. * @param string $text Text to search the phrase in
  195. * @param string $phrase The phrase that will be searched
  196. * @param array $options An array of html attributes and options.
  197. * @return string The highlighted text
  198. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::highlight
  199. */
  200. public function highlight($text, $phrase, $options = array()) {
  201. return $this->_String->highlight($text, $phrase, $options);
  202. }
  203. /**
  204. * @see String::stripLinks()
  205. *
  206. * @param string $text Text
  207. * @return string The text without links
  208. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::stripLinks
  209. */
  210. public function stripLinks($text) {
  211. return $this->_String->stripLinks($text);
  212. }
  213. /**
  214. * @see String::truncate()
  215. *
  216. * @param string $text String to truncate.
  217. * @param integer $length Length of returned string, including ellipsis.
  218. * @param array $options An array of html attributes and options.
  219. * @return string Trimmed string.
  220. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::truncate
  221. */
  222. public function truncate($text, $length = 100, $options = array()) {
  223. return $this->_String->truncate($text, $length, $options);
  224. }
  225. /**
  226. * @see String::excerpt()
  227. *
  228. * @param string $text String to search the phrase in
  229. * @param string $phrase Phrase that will be searched for
  230. * @param integer $radius The amount of characters that will be returned on each side of the founded phrase
  231. * @param string $ending Ending that will be appended
  232. * @return string Modified string
  233. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::excerpt
  234. */
  235. public function excerpt($text, $phrase, $radius = 100, $ending = '...') {
  236. return $this->_String->excerpt($text, $phrase, $radius, $ending);
  237. }
  238. /**
  239. * @see String::toList()
  240. *
  241. * @param array $list The list to be joined
  242. * @param string $and The word used to join the last and second last items together with. Defaults to 'and'
  243. * @param string $separator The separator used to join all the other items together. Defaults to ', '
  244. * @return string The glued together string.
  245. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::toList
  246. */
  247. public function toList($list, $and = 'and', $separator = ', ') {
  248. return $this->_String->toList($list, $and, $separator);
  249. }
  250. }