PageRenderTime 32ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/helper/TextHelper.php

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