[ Index ]
 

Code source de CakePHP 1.1.13.4450

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

title

Body

[fermer]

/cake/libs/view/helpers/ -> text.php (source)

   1  <?php
   2  /* SVN FILE: $Id: text.php 4409 2007-02-02 13:20:59Z phpnut $ */
   3  /**
   4   * Text Helper
   5   *
   6   * Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
   7   *
   8   * PHP versions 4 and 5
   9   *
  10   * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
  11   * Copyright 2005-2007, Cake Software Foundation, Inc.
  12   *                                1785 E. Sahara Avenue, Suite 490-204
  13   *                                Las Vegas, Nevada 89104
  14   *
  15   * Licensed under The MIT License
  16   * Redistributions of files must retain the above copyright notice.
  17   *
  18   * @filesource
  19   * @copyright        Copyright 2005-2007, Cake Software Foundation, Inc.
  20   * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  21   * @package            cake
  22   * @subpackage        cake.cake.libs.view.helpers
  23   * @since            CakePHP(tm) v 0.10.0.1076
  24   * @version            $Revision: 4409 $
  25   * @modifiedby        $LastChangedBy: phpnut $
  26   * @lastmodified    $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
  27   * @license            http://www.opensource.org/licenses/mit-license.php The MIT License
  28   */
  29  /**
  30   * Included libraries.
  31   *
  32   */
  33  if (!class_exists('Flay')) {
  34       uses('flay');
  35  }
  36  if (!class_exists('HtmlHelper')) {
  37       uses('view' . DS . 'helpers' . DS . 'html');
  38  }
  39  /**
  40   * Text helper library.
  41   *
  42   * Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
  43   *
  44   * @package        cake
  45   * @subpackage    cake.cake.libs.view.helpers
  46   */
  47  class TextHelper extends Helper{
  48  /**
  49   * Highlights a given phrase in a text.
  50   *
  51   * @param string $text Text to search the phrase in
  52   * @param string $phrase The phrase that will be searched
  53   * @param string $highlighter The piece of html with that the phrase will be highlighted
  54   * @return string The highlighted text
  55   * @access public
  56   */
  57  	 function highlight($text, $phrase, $highlighter = '<span class="highlight">\1</span>') {
  58            if (empty($phrase))
  59                  return $text;
  60  
  61            if (is_array($phrase)) {
  62                  $replace=array();
  63                  $with=array();
  64  
  65                  foreach($phrase as $key => $value) {
  66                       if (empty($key)) {
  67                            $key  =$value;
  68                            $value=$highlighter;
  69                       }
  70  
  71                       $replace[]='|(' . $key . ')|';
  72                       $with[]=empty($value) ? $highlighter : $value;
  73                  }
  74  
  75                  return preg_replace($replace, $with, $text);
  76            } else {
  77                  return preg_replace("|({$phrase})|i", $highlighter, $text);
  78            }
  79       }
  80  /**
  81   * Strips given text of all links (<a href=....)
  82   *
  83   * @param string $text Text
  84   * @return string The text without links
  85   * @access public
  86   */
  87  	 function stripLinks($text) {
  88            return preg_replace('|<a.*>(.*)<\/a>|im', '\1', $text);
  89       }
  90  /**
  91   * Adds links (<a href=....) to a given text, by finding text that begins with
  92   * strings like http:// and ftp://.
  93   *
  94   * @param string $text Text to add links to
  95   * @param array $htmlOptions Array of HTML options.
  96   * @return string The text with links
  97   * @access public
  98   */
  99  	 function autoLinkUrls($text, $htmlOptions = array()) {
 100            $options='array(';
 101  
 102            foreach($htmlOptions as $option => $value) {
 103                  $options .= "'$option' => '$value', ";
 104            }
 105  
 106            $options .= ')';
 107  
 108            $text = preg_replace_callback('#((?:http|https|ftp|nntp)://[^ <]+)#',
 109                                                  create_function('$matches',
 110                                                                       '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $options . ');'),
 111                                                  $text);
 112            return preg_replace_callback('#(?<!http://|https://|ftp://|nntp://)(www\.[^\n\%\ <]+[^<\n\%\,\.\ <])#',
 113                                                   create_function('$matches',
 114                                                                        '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "http://" . $matches[0],' . $options . ');'),
 115                                                   $text);
 116       }
 117  /**
 118   * Adds email links (<a href="mailto:....) to a given text.
 119   *
 120   * @param string $text Text
 121   * @param array $htmlOptions Array of HTML options.
 122   * @return string The text with links
 123   * @access public
 124   */
 125  	 function autoLinkEmails($text, $htmlOptions = array()) {
 126            $options='array(';
 127  
 128            foreach($htmlOptions as $option => $value) {
 129                  $options .= "'$option' => '$value', ";
 130            }
 131  
 132            $options .= ')';
 133  
 134            return preg_replace_callback(
 135                          '#([_A-Za-z0-9+-]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#',
 136                          create_function('$matches',
 137                                               '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->linkEmail($matches[0], $matches[0],' . $options . ');'),
 138                          $text);
 139       }
 140  /**
 141   * Convert all links and email adresses to HTML links.
 142   *
 143   * @param string $text Text
 144   * @param array $htmlOptions Array of HTML options.
 145   * @return string The text with links
 146   * @access public
 147   */
 148  	 function autoLink($text, $htmlOptions = array()) {
 149            return $this->autoLinkEmails($this->autoLinkUrls($text, $htmlOptions), $htmlOptions);
 150       }
 151  /**
 152   * Truncates text.
 153   *
 154   * Cuts a string to the length of $length and replaces the last characters
 155   * with the ending if the text is longer than length.
 156   *
 157   * @param string  $text    String to truncate.
 158   * @param integer $length Length of returned string, including ellipsis.
 159   * @param string  $ending Ending to be appended to the trimmed string.
 160   * @param boolean $exact If false, $test will not be cut mid-word
 161   * @return string Trimmed string.
 162   * @access public
 163   */
 164  	 function truncate($text, $length, $ending = '...', $exact = true) {
 165            if (strlen($text) <= $length) {
 166                  return $text;
 167            } else {
 168                  $truncate=substr($text, 0, $length - strlen($ending));
 169  
 170                  if (!$exact) {
 171                       $spacepos=strrpos($truncate, ' ');
 172  
 173                       if (isset($spacepos)) {
 174                            return substr($truncate, 0, $spacepos) . $ending;
 175                       }
 176                  }
 177  
 178                  return $truncate . $ending;
 179            }
 180       }
 181  /**
 182   * Alias for truncate().
 183   *
 184   * @see TextHelper::truncate()
 185   * @return Text::truncate()
 186   * @access public
 187   */
 188  	 function trim() {
 189            $args=func_get_args();
 190            return call_user_func_array(array(&$this,
 191                        "truncate"),       $args);
 192       }
 193  /**
 194   * Extracts an excerpt from the text surrounding the phrase with a number of characters on each side determined by radius.
 195   *
 196   * @param string $text String to search the phrase in
 197   * @param string $phrase Phrase that will be searched for
 198   * @param integer $radius The amount of characters that will be returned on each side of the founded phrase
 199   * @param string $ending Ending that will be appended
 200   * @return string
 201   * @access public
 202   */
 203  	 function excerpt($text, $phrase, $radius = 100, $ending = "...") {
 204            if (empty($text) or empty($phrase))
 205                  return $this->truncate($text, $radius * 2, $ending);
 206  
 207            if ($radius < strlen($phrase))
 208                  $radius=strlen($phrase);
 209  
 210            $pos     =strpos($text, $phrase);
 211            $startPos=$pos <= $radius ? 0 : $pos - $radius;
 212            $endPos  =$pos + strlen($phrase) + $radius >= strlen($text)
 213                ? strlen($text) : $pos + strlen($phrase) + $radius;
 214  
 215            $excerpt =substr($text, $startPos, $endPos - $startPos);
 216  
 217            if ($startPos != 0)
 218                  $excerpt=substr_replace($excerpt, $ending, 0, strlen($phrase));
 219  
 220            if ($endPos != strlen($text))
 221                  $excerpt=substr_replace($excerpt, $ending, -strlen($phrase));
 222  
 223            return $excerpt;
 224       }
 225  /**
 226   * Text-to-html parser, similar to Textile or RedCloth, only with a little different syntax.
 227   *
 228   * @param string $text String to "flay"
 229   * @param boolean $allowHtml Set to true if if html is allowed
 230   * @return string "Flayed" text
 231   * @todo Change this. We need a real Textile parser.
 232   * @access public
 233   */
 234  	 function flay($text, $allowHtml = false) {
 235            return Flay::toHtml($text, false, $allowHtml);
 236       }
 237  }
 238  ?>


Généré le : Sun Feb 25 19:27:47 2007 par Balluche grâce à PHPXref 0.7