[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/helper/ -> TextHelper.php (source)

   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 3408 2007-02-06 08:05:36Z 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    if (strlen($text) > $length)
  34    {
  35      $truncate_text = substr($text, 0, $length - strlen($truncate_string));
  36      if ($truncate_lastspace)
  37      {
  38        $truncate_text = preg_replace('/\s+?(\S+)?$/', '', $truncate_text);
  39      }
  40  
  41      return $truncate_text.$truncate_string;
  42    }
  43    else
  44    {
  45      return $text;
  46    }
  47  }
  48  
  49  /**
  50   * Highlights the +phrase+ where it is found in the +text+ by surrounding it like
  51   * <strong class="highlight">I'm a highlight phrase</strong>. The highlighter can be specialized by
  52   * passing +highlighter+ as single-quoted string with \1 where the phrase is supposed to be inserted.
  53   * N.B.: The +phrase+ is sanitized to include only letters, digits, and spaces before use.
  54   */
  55  function highlight_text($text, $phrase, $highlighter = '<strong class="highlight">\\1</strong>')
  56  {
  57    if ($text == '')
  58    {
  59      return '';
  60    }
  61  
  62    if ($phrase == '')
  63    {
  64      return $text;
  65    }
  66  
  67    return preg_replace('/('.preg_quote($phrase).')/i', $highlighter, $text);
  68  }
  69  
  70  /**
  71   * Extracts an excerpt from the +text+ surrounding the +phrase+ with a number of characters on each side determined
  72   * by +radius+. If the phrase isn't found, nil is returned. Ex:
  73   *   excerpt("hello my world", "my", 3) => "...lo my wo..."
  74   */
  75  function excerpt_text($text, $phrase, $radius = 100, $excerpt_string = '...')
  76  {
  77    if ($text == '' || $phrase == '')
  78    {
  79      return '';
  80    }
  81  
  82    $phrase = preg_quote($phrase);
  83  
  84    $found_pos = strpos(strtolower($text), strtolower($phrase));
  85    if ($found_pos !== false)
  86    {
  87      $start_pos = max($found_pos - $radius, 0);
  88      $end_pos = min($found_pos + strlen($phrase) + $radius, strlen($text));
  89  
  90      $prefix = ($start_pos > 0) ? $excerpt_string : '';
  91      $postfix = $end_pos < strlen($text) ? $excerpt_string : '';
  92  
  93      return $prefix.substr($text, $start_pos, $end_pos - $start_pos).$postfix;
  94    }
  95  }
  96  
  97  /**
  98   * Word wrap long lines to line_width.
  99   */
 100  function wrap_text($text, $line_width = 80)
 101  {
 102    return preg_replace('/(.{1,'.$line_width.'})(\s+|$)/s', "\\1\n", preg_replace("/\n/", "\n\n", $text));
 103  }
 104  
 105  /*
 106      # Returns +text+ transformed into html using very simple formatting rules
 107      # Surrounds paragraphs with <tt>&lt;p&gt;</tt> tags, and converts line breaks into <tt>&lt;br /&gt;</tt>
 108      # Two consecutive newlines(<tt>\n\n</tt>) are considered as a paragraph, one newline (<tt>\n</tt>) is
 109      # considered a linebreak, three or more consecutive newlines are turned into two newlines
 110  */
 111  function simple_format_text($text, $options = array())
 112  {
 113    $css = (isset($options['class'])) ? ' class="'.$options['class'].'"' : '';
 114  
 115    $text = sfToolkit::pregtr($text, array("/(\r\n|\r)/"        => "\n",               // lets make them newlines crossplatform
 116                                           "/\n{3,}/"           => "\n\n",             // zap dupes
 117                                           "/\n\n/"             => "</p>\\0<p$css>",   // turn two newlines into paragraph
 118                                           "/([^\n])\n([^\n])/" => "\\1\n<br />\\2")); // turn single newline into <br/>
 119  
 120    return '<p'.$css.'>'.$text.'</p>'; // wrap the first and last line in paragraphs before we're done
 121  }
 122  
 123  /**
 124   * Turns all urls and email addresses into clickable links. The +link+ parameter can limit what should be linked.
 125   * Options are :all (default), :email_addresses, and :urls.
 126   *
 127   * Example:
 128   *   auto_link("Go to http://www.symfony-project.com and say hello to fabien.potencier@example.com") =>
 129   *     Go to <a href="http://www.symfony-project.com">http://www.symfony-project.com</a> and
 130   *     say hello to <a href="mailto:fabien.potencier@example.com">fabien.potencier@example.com</a>
 131   */
 132  function auto_link_text($text, $link = 'all', $href_options = array())
 133  {
 134    if ($link == 'all')
 135    {
 136      return _auto_link_urls(_auto_link_email_addresses($text), $href_options);
 137    }
 138    else if ($link == 'email_addresses')
 139    {
 140      return _auto_link_email_addresses($text);
 141    }
 142    else if ($link == 'urls')
 143    {
 144      return _auto_link_urls($text, $href_options);
 145    }
 146  }
 147  
 148  /*
 149   * Turns all links into words, like "<a href="something">else</a>" to "else".
 150   */
 151  function strip_links_text($text)
 152  {
 153    return preg_replace('/<a.*>(.*)<\/a>/m', '\\1', $text);
 154  }
 155  
 156  if (!defined('SF_AUTO_LINK_RE'))
 157  {
 158    define('SF_AUTO_LINK_RE', '~
 159      (                       # leading text
 160        <\w+.*?>|             #   leading HTML tag, or
 161        [^=!:\'"/]|           #   leading punctuation, or
 162        ^                     #   beginning of line
 163      )
 164      (
 165        (?:https?://)|        # protocol spec, or
 166        (?:www\.)             # www.*
 167      )
 168      (
 169        [-\w]+                   # subdomain or domain
 170        (?:\.[-\w]+)*            # remaining subdomains or domain
 171        (?::\d+)?                # port
 172        (?:/(?:(?:[\~\w\+%-]|(?:[,.;:][^\s$]))+)?)* # path
 173        (?:\?[\w\+%&=.;-]+)?     # query string
 174        (?:\#[\w\-]*)?           # trailing anchor
 175      )
 176      ([[:punct:]]|\s|<|$)    # trailing text
 177     ~x');
 178  }
 179  
 180  /**
 181   * Turns all urls into clickable links.
 182   */
 183  function _auto_link_urls($text, $href_options = array())
 184  {
 185    $href_options = _tag_options($href_options);
 186    return preg_replace_callback(
 187      SF_AUTO_LINK_RE,
 188      create_function('$matches', '
 189        if (preg_match("/<a\s/i", $matches[1]))
 190        {
 191          return $matches[0];
 192        }
 193        else
 194        {
 195          return $matches[1].\'<a href="\'.($matches[2] == "www." ? "http://www." : $matches[2]).$matches[3].\'"'.$href_options.'>\'.$matches[2].$matches[3].\'</a>\'.$matches[4];
 196        }
 197      ')
 198    , $text);
 199  }
 200  
 201  /**
 202   * Turns all email addresses into clickable links.
 203   */
 204  function _auto_link_email_addresses($text)
 205  {
 206    return preg_replace('/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/', '<a href="mailto:\\1">\\1</a>', $text);
 207  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7