Development

/branches/1.1/lib/helper/TextHelper.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/helper/TextHelper.php

Revision 9778, 8.5 kB (checked in by dwhittle, 7 years ago)

1.1: added support for highlighting multiple phrases in highlight_test (closes #1520)

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