TextHelper.php 7.8 KB

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