PageRenderTime 60ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/urlcatcher.org/htdocs/lib/vendor/symfony/lib/helper/TextHelper.php

https://github.com/bigcalm/urlcatcher
PHP | 276 lines | 178 code | 28 blank | 70 comment | 27 complexity | fb7fb93ce7ad1a7bd93b221c6a6229a9 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004 David Heinemeier Hansson
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * TextHelper.
  12. *
  13. * @package symfony
  14. * @subpackage helper
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @author David Heinemeier Hansson
  17. * @version SVN: $Id: TextHelper.php 20142 2009-07-13 10:20:44Z FabianLange $
  18. */
  19. /**
  20. * Truncates +text+ to the length of +length+ and replaces the last three characters with the +truncate_string+
  21. * if the +text+ is longer than +length+.
  22. */
  23. function truncate_text($text, $length = 30, $truncate_string = '...', $truncate_lastspace = false)
  24. {
  25. if ($text == '')
  26. {
  27. return '';
  28. }
  29. $mbstring = extension_loaded('mbstring');
  30. if($mbstring)
  31. {
  32. @mb_internal_encoding(mb_detect_encoding($text));
  33. }
  34. $strlen = ($mbstring) ? 'mb_strlen' : 'strlen';
  35. $substr = ($mbstring) ? 'mb_substr' : 'substr';
  36. if ($strlen($text) > $length)
  37. {
  38. $truncate_text = $substr($text, 0, $length - $strlen($truncate_string));
  39. if ($truncate_lastspace)
  40. {
  41. $truncate_text = preg_replace('/\s+?(\S+)?$/', '', $truncate_text);
  42. }
  43. return $truncate_text.$truncate_string;
  44. }
  45. else
  46. {
  47. return $text;
  48. }
  49. }
  50. /**
  51. * Highlights the +phrase+ where it is found in the +text+ by surrounding it like
  52. * <strong class="highlight">I'm a highlight phrase</strong>. The highlighter can be specialized by
  53. * passing +highlighter+ as single-quoted string with \1 where the phrase is supposed to be inserted.
  54. * N.B.: The +phrase+ is sanitized to include only letters, digits, and spaces before use.
  55. *
  56. * @param string $text subject input to preg_replace.
  57. * @param string $phrase string or array of words to highlight
  58. * @param string $highlighter regex replacement input to preg_replace.
  59. *
  60. * @return string
  61. */
  62. function highlight_text($text, $phrase, $highlighter = '<strong class="highlight">\\1</strong>')
  63. {
  64. if (empty($text))
  65. {
  66. return '';
  67. }
  68. if (empty($phrase))
  69. {
  70. return $text;
  71. }
  72. if (is_array($phrase) or ($phrase instanceof sfOutputEscaperArrayDecorator))
  73. {
  74. foreach ($phrase as $word)
  75. {
  76. $pattern[] = '/('.preg_quote($word, '/').')/i';
  77. $replacement[] = $highlighter;
  78. }
  79. }
  80. else
  81. {
  82. $pattern = '/('.preg_quote($phrase, '/').')/i';
  83. $replacement = $highlighter;
  84. }
  85. return preg_replace($pattern, $replacement, $text);
  86. }
  87. /**
  88. * Extracts an excerpt from the +text+ surrounding the +phrase+ with a number of characters on each side determined
  89. * by +radius+. If the phrase isn't found, nil is returned. Ex:
  90. * excerpt("hello my world", "my", 3) => "...lo my wo..."
  91. * If +excerpt_space+ is true the text will only be truncated on whitespace, never inbetween words.
  92. * This might return a smaller radius than specified.
  93. * excerpt("hello my world", "my", 3, "...", true) => "... my ..."
  94. */
  95. function excerpt_text($text, $phrase, $radius = 100, $excerpt_string = '...', $excerpt_space = false)
  96. {
  97. if ($text == '' || $phrase == '')
  98. {
  99. return '';
  100. }
  101. $mbstring = extension_loaded('mbstring');
  102. if($mbstring)
  103. {
  104. @mb_internal_encoding(mb_detect_encoding($text));
  105. }
  106. $strlen = ($mbstring) ? 'mb_strlen' : 'strlen';
  107. $strpos = ($mbstring) ? 'mb_strpos' : 'strpos';
  108. $strtolower = ($mbstring) ? 'mb_strtolower' : 'strtolower';
  109. $substr = ($mbstring) ? 'mb_substr' : 'substr';
  110. $found_pos = $strpos($strtolower($text), $strtolower($phrase));
  111. if ($found_pos !== false)
  112. {
  113. $start_pos = max($found_pos - $radius, 0);
  114. $end_pos = min($found_pos + $strlen($phrase) + $radius, $strlen($text));
  115. $excerpt = $substr($text, $start_pos, $end_pos - $start_pos);
  116. $prefix = ($start_pos > 0) ? $excerpt_string : '';
  117. $postfix = $end_pos < $strlen($text) ? $excerpt_string : '';
  118. if ($excerpt_space)
  119. {
  120. // only cut off at ends where $exceprt_string is added
  121. if($prefix)
  122. {
  123. $excerpt = preg_replace('/^(\S+)?\s+?/', ' ', $excerpt);
  124. }
  125. if($postfix)
  126. {
  127. $excerpt = preg_replace('/\s+?(\S+)?$/', ' ', $excerpt);
  128. }
  129. }
  130. return $prefix.$excerpt.$postfix;
  131. }
  132. }
  133. /**
  134. * Word wrap long lines to line_width.
  135. */
  136. function wrap_text($text, $line_width = 80)
  137. {
  138. return preg_replace('/(.{1,'.$line_width.'})(\s+|$)/s', "\\1\n", preg_replace("/\n/", "\n\n", $text));
  139. }
  140. /**
  141. * Returns +text+ transformed into html using very simple formatting rules
  142. * Surrounds paragraphs with <tt>&lt;p&gt;</tt> tags, and converts line breaks into <tt>&lt;br /&gt;</tt>
  143. * Two consecutive newlines(<tt>\n\n</tt>) are considered as a paragraph, one newline (<tt>\n</tt>) is
  144. * considered a linebreak, three or more consecutive newlines are turned into two newlines
  145. */
  146. function simple_format_text($text, $options = array())
  147. {
  148. $css = (isset($options['class'])) ? ' class="'.$options['class'].'"' : '';
  149. $text = sfToolkit::pregtr($text, array("/(\r\n|\r)/" => "\n", // lets make them newlines crossplatform
  150. "/\n{2,}/" => "</p><p$css>")); // turn two and more newlines into paragraph
  151. // turn single newline into <br/>
  152. $text = str_replace("\n", "\n<br />", $text);
  153. return '<p'.$css.'>'.$text.'</p>'; // wrap the first and last line in paragraphs before we're done
  154. }
  155. /**
  156. * Turns all urls and email addresses into clickable links. The +link+ parameter can limit what should be linked.
  157. * Options are :all (default), :email_addresses, and :urls.
  158. *
  159. * Example:
  160. * auto_link("Go to http://www.symfony-project.com and say hello to fabien.potencier@example.com") =>
  161. * Go to <a href="http://www.symfony-project.com">http://www.symfony-project.com</a> and
  162. * say hello to <a href="mailto:fabien.potencier@example.com">fabien.potencier@example.com</a>
  163. */
  164. function auto_link_text($text, $link = 'all', $href_options = array(), $truncate = false, $truncate_len = 35, $pad = '...')
  165. {
  166. if ($link == 'all')
  167. {
  168. return _auto_link_urls(_auto_link_email_addresses($text), $href_options, $truncate, $truncate_len, $pad);
  169. }
  170. else if ($link == 'email_addresses')
  171. {
  172. return _auto_link_email_addresses($text);
  173. }
  174. else if ($link == 'urls')
  175. {
  176. return _auto_link_urls($text, $href_options, $truncate, $truncate_len, $pad);
  177. }
  178. }
  179. /**
  180. * Turns all links into words, like "<a href="something">else</a>" to "else".
  181. */
  182. function strip_links_text($text)
  183. {
  184. return preg_replace('/<a.*>(.*)<\/a>/m', '\\1', $text);
  185. }
  186. if (!defined('SF_AUTO_LINK_RE'))
  187. {
  188. define('SF_AUTO_LINK_RE', '~
  189. ( # leading text
  190. <\w+.*?>| # leading HTML tag, or
  191. [^=!:\'"/]| # leading punctuation, or
  192. ^ # beginning of line
  193. )
  194. (
  195. (?:https?://)| # protocol spec, or
  196. (?:www\.) # www.*
  197. )
  198. (
  199. [-\w]+ # subdomain or domain
  200. (?:\.[-\w]+)* # remaining subdomains or domain
  201. (?::\d+)? # port
  202. (?:/(?:(?:[\~\w\+%-]|(?:[,.;:][^\s$]))+)?)* # path
  203. (?:\?[\w\+%&=.;-]+)? # query string
  204. (?:\#[\w\-]*)? # trailing anchor
  205. )
  206. ([[:punct:]]|\s|<|$) # trailing text
  207. ~x');
  208. }
  209. /**
  210. * Turns all urls into clickable links.
  211. */
  212. function _auto_link_urls($text, $href_options = array(), $truncate = false, $truncate_len = 40, $pad = '...')
  213. {
  214. $href_options = _tag_options($href_options);
  215. $callback_function = '
  216. if (preg_match("/<a\s/i", $matches[1]))
  217. {
  218. return $matches[0];
  219. }
  220. ';
  221. if ($truncate)
  222. {
  223. $callback_function .= '
  224. else if (strlen($matches[2].$matches[3]) > '.$truncate_len.')
  225. {
  226. return $matches[1].\'<a href="\'.($matches[2] == "www." ? "http://www." : $matches[2]).$matches[3].\'"'.$href_options.'>\'.substr($matches[2].$matches[3], 0, '.$truncate_len.').\''.$pad.'</a>\'.$matches[4];
  227. }
  228. ';
  229. }
  230. $callback_function .= '
  231. else
  232. {
  233. return $matches[1].\'<a href="\'.($matches[2] == "www." ? "http://www." : $matches[2]).$matches[3].\'"'.$href_options.'>\'.$matches[2].$matches[3].\'</a>\'.$matches[4];
  234. }
  235. ';
  236. return preg_replace_callback(
  237. SF_AUTO_LINK_RE,
  238. create_function('$matches', $callback_function),
  239. $text
  240. );
  241. }
  242. /**
  243. * Turns all email addresses into clickable links.
  244. */
  245. function _auto_link_email_addresses($text)
  246. {
  247. return preg_replace('/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/', '<a href="mailto:\\1">\\1</a>', $text);
  248. }