text.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: text_8php-source.html 675 2008-12-26 00:27:14Z gwoo $ */
00003 /**
00004  * Text Helper
00005  *
00006  * Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.libs.view.helpers
00023  * @since           CakePHP(tm) v 0.10.0.1076
00024  * @version         $Revision: 675 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 /**
00030  * Included libraries.
00031  *
00032  */
00033 if (!class_exists('Flay')) {
00034      uses('flay');
00035 }
00036 if (!class_exists('HtmlHelper')) {
00037      uses('view' . DS . 'helpers' . DS . 'html');
00038 }
00039 /**
00040  * Text helper library.
00041  *
00042  * Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
00043  *
00044  * @package     cake
00045  * @subpackage  cake.cake.libs.view.helpers
00046  */
00047 class TextHelper extends Helper{
00048 /**
00049  * Highlights a given phrase in a text.
00050  *
00051  * @param string $text Text to search the phrase in
00052  * @param string $phrase The phrase that will be searched
00053  * @param string $highlighter The piece of html with that the phrase will be highlighted
00054  * @return string The highlighted text
00055  * @access public
00056  */
00057      function highlight($text, $phrase, $highlighter = '<span class="highlight">\1</span>') {
00058           if (empty($phrase))
00059                 return $text;
00060 
00061           if (is_array($phrase)) {
00062                 $replace=array();
00063                 $with=array();
00064 
00065                 foreach ($phrase as $key => $value) {
00066                      if (empty($key)) {
00067                           $key  =$value;
00068                           $value=$highlighter;
00069                      }
00070 
00071                      $replace[]='|(' . $key . ')|i';
00072                      $with[]=empty($value) ? $highlighter : $value;
00073                 }
00074 
00075                 return preg_replace($replace, $with, $text);
00076           } else {
00077                 return preg_replace("|({$phrase})|i", $highlighter, $text);
00078           }
00079      }
00080 /**
00081  * Strips given text of all links (<a href=....)
00082  *
00083  * @param string $text Text
00084  * @return string The text without links
00085  * @access public
00086  */
00087      function stripLinks($text) {
00088           return preg_replace('|<a.*>(.*)<\/a>|im', '\1', $text);
00089      }
00090 /**
00091  * Adds links (<a href=....) to a given text, by finding text that begins with
00092  * strings like http:// and ftp://.
00093  *
00094  * @param string $text Text to add links to
00095  * @param array $htmlOptions Array of HTML options.
00096  * @return string The text with links
00097  * @access public
00098  */
00099      function autoLinkUrls($text, $htmlOptions = array()) {
00100           $options='array(';
00101 
00102           foreach ($htmlOptions as $option => $value) {
00103                 $options .= "'$option' => '$value', ";
00104           }
00105 
00106           $options .= ')';
00107 
00108           $text = preg_replace_callback('#(?<!href="|">)((?:http|https|ftp|nntp)://[^ <]+)#i',
00109                                                 create_function('$matches',
00110                                                                      '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $options . ');'),
00111                                                 $text);
00112           return preg_replace_callback('#(?<!href="|">)(?<!http://|https://|ftp://|nntp://)(www\.[^\n\%\ <]+[^<\n\%\,\.\ <])#i',
00113                                                  create_function('$matches',
00114                                                                       '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "http://" . $matches[0],' . $options . ');'),
00115                                                  $text);
00116      }
00117 /**
00118  * Adds email links (<a  href="/page/mailto:....) to a given text.
00119  *
00120  * @param string $text Text
00121  * @param array $htmlOptions Array of HTML options.
00122  * @return string The text with links
00123  * @access public
00124  */
00125      function autoLinkEmails($text, $htmlOptions = array()) {
00126           $options='array(';
00127 
00128           foreach ($htmlOptions as $option => $value) {
00129                 $options .= "'$option' => '$value', ";
00130           }
00131 
00132           $options .= ')';
00133 
00134           return preg_replace_callback(
00135                         '#([_A-Za-z0-9+-]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#',
00136                         create_function('$matches',
00137                                              '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->linkEmail($matches[0], $matches[0],' . $options . ');'),
00138                         $text);
00139      }
00140 /**
00141  * Convert all links and email adresses to HTML links.
00142  *
00143  * @param string $text Text
00144  * @param array $htmlOptions Array of HTML options.
00145  * @return string The text with links
00146  * @access public
00147  */
00148      function autoLink($text, $htmlOptions = array()) {
00149           return $this->autoLinkEmails($this->autoLinkUrls($text, $htmlOptions), $htmlOptions);
00150      }
00151 /**
00152  * Truncates text.
00153  *
00154  * Cuts a string to the length of $length and replaces the last characters
00155  * with the ending if the text is longer than length.
00156  *
00157  * @param string  $text String to truncate.
00158  * @param integer $length Length of returned string, including ellipsis.
00159  * @param string  $ending Ending to be appended to the trimmed string.
00160  * @param boolean $exact If false, $test will not be cut mid-word
00161  * @return string Trimmed string.
00162  * @access public
00163  */
00164      function truncate($text, $length, $ending = '...', $exact = true) {
00165           if (strlen($text) <= $length) {
00166                 return $text;
00167           } else {
00168                 $truncate=substr($text, 0, $length - strlen($ending));
00169 
00170                 if (!$exact) {
00171                      $spacepos=strrpos($truncate, ' ');
00172 
00173                      if (isset($spacepos)) {
00174                           return substr($truncate, 0, $spacepos) . $ending;
00175                      }
00176                 }
00177 
00178                 return $truncate . $ending;
00179           }
00180      }
00181 /**
00182  * Alias for truncate().
00183  *
00184  * @see TextHelper::truncate()
00185  * @return Text::truncate()
00186  * @access public
00187  */
00188      function trim() {
00189           $args=func_get_args();
00190           return call_user_func_array(array(&$this,
00191                       "truncate"),       $args);
00192      }
00193 /**
00194  * Extracts an excerpt from the text surrounding the phrase with a number of characters on each side determined by radius.
00195  *
00196  * @param string $text String to search the phrase in
00197  * @param string $phrase Phrase that will be searched for
00198  * @param integer $radius The amount of characters that will be returned on each side of the founded phrase
00199  * @param string $ending Ending that will be appended
00200  * @return string
00201  * @access public
00202  */
00203      function excerpt($text, $phrase, $radius = 100, $ending = "...") {
00204           if (empty($text) or empty($phrase))
00205                 return $this->truncate($text, $radius * 2, $ending);
00206 
00207           if ($radius < strlen($phrase))
00208                 $radius=strlen($phrase);
00209 
00210           $pos     =strpos($text, $phrase);
00211           $startPos=$pos <= $radius ? 0 : $pos - $radius;
00212           $endPos  =$pos + strlen($phrase) + $radius >= strlen($text)
00213               ? strlen($text) : $pos + strlen($phrase) + $radius;
00214 
00215           $excerpt =substr($text, $startPos, $endPos - $startPos);
00216 
00217           if ($startPos != 0)
00218                 $excerpt=substr_replace($excerpt, $ending, 0, strlen($phrase));
00219 
00220           if ($endPos != strlen($text))
00221                 $excerpt=substr_replace($excerpt, $ending, -strlen($phrase));
00222 
00223           return $excerpt;
00224      }
00225 /**
00226  * Text-to-html parser, similar to Textile or RedCloth, only with a little different syntax.
00227  *
00228  * @param string $text String to "flay"
00229  * @param boolean $allowHtml Set to true if if html is allowed
00230  * @return string "Flayed" text
00231  * @todo Change this. We need a real Textile parser.
00232  * @access public
00233  */
00234      function flay($text, $allowHtml = false) {
00235           return Flay::toHtml($text, false, $allowHtml);
00236      }
00237 }
00238 ?>