Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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