text_helper.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2006, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * CodeIgniter Text Helpers
00020  *
00021  * @package             CodeIgniter
00022  * @subpackage  Helpers
00023  * @category    Helpers
00024  * @author              ExpressionEngine Dev Team
00025  * @link                http://codeigniter.com/user_guide/helpers/text_helper.html
00026  */
00027 
00028 // ------------------------------------------------------------------------
00029 
00030 /**
00031  * Word Limiter
00032  *
00033  * Limits a string to X number of words.
00034  *
00035  * @access      public
00036  * @param       string
00037  * @param       integer
00038  * @param       string  the end character. Usually an ellipsis
00039  * @return      string
00040  */     
00041 if ( ! function_exists('word_limiter'))
00042 {
00043         function word_limiter($str, $limit = 100, $end_char = '&#8230;')
00044         {
00045                 if (trim($str) == '')
00046                 {
00047                         return $str;
00048                 }
00049         
00050                 preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
00051                         
00052                 if (strlen($str) == strlen($matches[0]))
00053                 {
00054                         $end_char = '';
00055                 }
00056                 
00057                 return rtrim($matches[0]).$end_char;
00058         }
00059 }
00060         
00061 // ------------------------------------------------------------------------
00062 
00063 /**
00064  * Character Limiter
00065  *
00066  * Limits the string based on the character count.  Preserves complete words
00067  * so the character count may not be exactly as specified.
00068  *
00069  * @access      public
00070  * @param       string
00071  * @param       integer
00072  * @param       string  the end character. Usually an ellipsis
00073  * @return      string
00074  */     
00075 if ( ! function_exists('character_limiter'))
00076 {
00077         function character_limiter($str, $n = 500, $end_char = '&#8230;')
00078         {
00079                 if (strlen($str) < $n)
00080                 {
00081                         return $str;
00082                 }
00083                 
00084                 $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
00085 
00086                 if (strlen($str) <= $n)
00087                 {
00088                         return $str;
00089                 }
00090                                                                         
00091                 $out = "";
00092                 foreach (explode(' ', trim($str)) as $val)
00093                 {
00094                         $out .= $val.' ';                       
00095                         if (strlen($out) >= $n)
00096                         {
00097                                 return trim($out).$end_char;
00098                         }               
00099                 }
00100         }
00101 }
00102         
00103 // ------------------------------------------------------------------------
00104 
00105 /**
00106  * High ASCII to Entities
00107  *
00108  * Converts High ascii text and MS Word special characters to character entities
00109  *
00110  * @access      public
00111  * @param       string
00112  * @return      string
00113  */     
00114 if ( ! function_exists('ascii_to_entities'))
00115 {
00116         function ascii_to_entities($str)
00117         {
00118            $count       = 1;
00119            $out = '';
00120            $temp        = array();
00121         
00122            for ($i = 0, $s = strlen($str); $i < $s; $i++)
00123            {
00124                    $ordinal = ord($str[$i]);
00125         
00126                    if ($ordinal < 128)
00127                    {
00128                            $out .= $str[$i];
00129                    }
00130                    else
00131                    {
00132                            if (count($temp) == 0)
00133                            {
00134                                    $count = ($ordinal < 224) ? 2 : 3;
00135                            }
00136                 
00137                            $temp[] = $ordinal;
00138                 
00139                            if (count($temp) == $count)
00140                            {
00141                                    $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
00142 
00143                                    $out .= '&#'.$number.';';
00144                                    $count = 1;
00145                                    $temp = array();
00146                            }
00147                    }
00148            }
00149 
00150            return $out;
00151         }
00152 }
00153         
00154 // ------------------------------------------------------------------------
00155 
00156 /**
00157  * Entities to ASCII
00158  *
00159  * Converts character entities back to ASCII
00160  *
00161  * @access      public
00162  * @param       string
00163  * @param       bool
00164  * @return      string
00165  */     
00166 if ( ! function_exists('entities_to_ascii'))
00167 {
00168         function entities_to_ascii($str, $all = TRUE)
00169         {
00170            if (preg_match_all('/\&#(\d+)\;/', $str, $matches))
00171            {
00172                    for ($i = 0, $s = count($matches['0']); $i < $s; $i++)
00173                    {                            
00174                            $digits = $matches['1'][$i];
00175 
00176                            $out = '';
00177 
00178                            if ($digits < 128)
00179                            {
00180                                    $out .= chr($digits);
00181                 
00182                            }
00183                            elseif ($digits < 2048)
00184                            {
00185                                    $out .= chr(192 + (($digits - ($digits % 64)) / 64));
00186                                    $out .= chr(128 + ($digits % 64));
00187                            }
00188                            else
00189                            {
00190                                    $out .= chr(224 + (($digits - ($digits % 4096)) / 4096));
00191                                    $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64));
00192                                    $out .= chr(128 + ($digits % 64));
00193                            }
00194 
00195                            $str = str_replace($matches['0'][$i], $out, $str);                           
00196                    }
00197            }
00198 
00199            if ($all)
00200            {
00201                    $str = str_replace(array("&amp;", "&lt;", "&gt;", "&quot;", "&apos;", "&#45;"),
00202                                                           array("&","<",">","\"", "'", "-"),
00203                                                           $str);
00204            }
00205 
00206            return $str;
00207         }
00208 }
00209         
00210 // ------------------------------------------------------------------------
00211 
00212 /**
00213  * Word Censoring Function
00214  *
00215  * Supply a string and an array of disallowed words and any
00216  * matched words will be converted to #### or to the replacement
00217  * word you've submitted.
00218  *
00219  * @access      public
00220  * @param       string  the text string
00221  * @param       string  the array of censoered words
00222  * @param       string  the optional replacement value
00223  * @return      string
00224  */     
00225 if ( ! function_exists('word_censor'))
00226 {
00227         function word_censor($str, $censored, $replacement = '')
00228         {
00229                 if ( ! is_array($censored))
00230                 {
00231                         return $str;
00232                 }
00233 
00234                 $str = ' '.$str.' ';
00235                 foreach ($censored as $badword)
00236                 {
00237                         if ($replacement != '')
00238                         {
00239                                 $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str);
00240                         }
00241                         else
00242                         {
00243                                 $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str);
00244                         }
00245                 }
00246         
00247                 return trim($str);
00248         }
00249 }
00250         
00251 // ------------------------------------------------------------------------
00252 
00253 /**
00254  * Code Highlighter
00255  *
00256  * Colorizes code strings
00257  *
00258  * @access      public
00259  * @param       string  the text string
00260  * @return      string
00261  */     
00262 if ( ! function_exists('highlight_code'))
00263 {
00264         function highlight_code($str)
00265         {               
00266                 // The highlight string function encodes and highlights
00267                 // brackets so we need them to start raw
00268                 $str = str_replace(array('&lt;', '&gt;'), array('<', '>'), $str);
00269         
00270                 // Replace any existing PHP tags to temporary markers so they don't accidentally
00271                 // break the string out of PHP, and thus, thwart the highlighting.
00272         
00273                 $str = str_replace(array('<?', '?>', '<%', '%>', '\\', '</script>'), 
00274                                                         array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str);
00275 
00276                 // The highlight_string function requires that the text be surrounded
00277                 // by PHP tags.  Since we don't know if A) the submitted text has PHP tags,
00278                 // or B) whether the PHP tags enclose the entire string, we will add our
00279                 // own PHP tags around the string along with some markers to make replacement easier later
00280         
00281                 $str = '<?php tempstart'."\n".$str.'tempend ?>';
00282         
00283                 // All the magic happens here, baby!
00284                 $str = highlight_string($str, TRUE);
00285 
00286                 // Prior to PHP 5, the highlight function used icky font tags
00287                 // so we'll replace them with span tags.        
00288                 if (abs(phpversion()) < 5)
00289                 {
00290                         $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
00291                         $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
00292                 }
00293         
00294                 // Remove our artificially added PHP
00295                 $str = preg_replace("#<code>.+?tempstart<br />(?:</span>)?#is", "<code>\n", $str);
00296                 $str = preg_replace("#tempend.+#is", "</span>\n</code>", $str); 
00297         
00298                 // Replace our markers back to PHP tags.
00299                 $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
00300                                                         array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'), $str);
00301                                                                                 
00302                 return $str;
00303         }
00304 }
00305         
00306 // ------------------------------------------------------------------------
00307 
00308 /**
00309  * Phrase Highlighter
00310  *
00311  * Highlights a phrase within a text string
00312  *
00313  * @access      public
00314  * @param       string  the text string
00315  * @param       string  the phrase you'd like to highlight
00316  * @param       string  the openging tag to precede the phrase with
00317  * @param       string  the closing tag to end the phrase with
00318  * @return      string
00319  */     
00320 if ( ! function_exists('highlight_phrase'))
00321 {
00322         function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
00323         {
00324                 if ($str == '')
00325                 {
00326                         return '';
00327                 }
00328         
00329                 if ($phrase != '')
00330                 {
00331                         return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str);
00332                 }
00333 
00334                 return $str;
00335         }
00336 }
00337         
00338 // ------------------------------------------------------------------------
00339 
00340 /**
00341  * Word Wrap
00342  *
00343  * Wraps text at the specified character.  Maintains the integrity of words.
00344  * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
00345  * will URLs.
00346  *
00347  * @access      public
00348  * @param       string  the text string
00349  * @param       integer the number of characters to wrap at
00350  * @return      string
00351  */     
00352 if ( ! function_exists('word_wrap'))
00353 {
00354         function word_wrap($str, $charlim = '76')
00355         {
00356                 // Se the character limit
00357                 if ( ! is_numeric($charlim))
00358                         $charlim = 76;
00359         
00360                 // Reduce multiple spaces
00361                 $str = preg_replace("| +|", " ", $str);
00362         
00363                 // Standardize newlines
00364                 if (strpos($str, "\r") !== FALSE)
00365                 {
00366                         $str = str_replace(array("\r\n", "\r"), "\n", $str);                    
00367                 }
00368         
00369                 // If the current word is surrounded by {unwrap} tags we'll 
00370                 // strip the entire chunk and replace it with a marker.
00371                 $unwrap = array();
00372                 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
00373                 {
00374                         for ($i = 0; $i < count($matches['0']); $i++)
00375                         {
00376                                 $unwrap[] = $matches['1'][$i];                          
00377                                 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
00378                         }
00379                 }
00380         
00381                 // Use PHP's native function to do the initial wordwrap.  
00382                 // We set the cut flag to FALSE so that any individual words that are 
00383                 // too long get left alone.  In the next step we'll deal with them.
00384                 $str = wordwrap($str, $charlim, "\n", FALSE);
00385         
00386                 // Split the string into individual lines of text and cycle through them
00387                 $output = "";
00388                 foreach (explode("\n", $str) as $line) 
00389                 {
00390                         // Is the line within the allowed character count?
00391                         // If so we'll join it to the output and continue
00392                         if (strlen($line) <= $charlim)
00393                         {
00394                                 $output .= $line."\n";                  
00395                                 continue;
00396                         }
00397                         
00398                         $temp = '';
00399                         while((strlen($line)) > $charlim) 
00400                         {
00401                                 // If the over-length word is a URL we won't wrap it
00402                                 if (preg_match("!\[url.+\]|://|wwww.!", $line))
00403                                 {
00404                                         break;
00405                                 }
00406 
00407                                 // Trim the word down
00408                                 $temp .= substr($line, 0, $charlim-1);
00409                                 $line = substr($line, $charlim-1);
00410                         }
00411                 
00412                         // If $temp contains data it means we had to split up an over-length 
00413                         // word into smaller chunks so we'll add it back to our current line
00414                         if ($temp != '')
00415                         {
00416                                 $output .= $temp . "\n" . $line; 
00417                         }
00418                         else
00419                         {
00420                                 $output .= $line;
00421                         }
00422 
00423                         $output .= "\n";
00424                 }
00425 
00426                 // Put our markers back
00427                 if (count($unwrap) > 0)
00428                 {       
00429                         foreach ($unwrap as $key => $val)
00430                         {
00431                                 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
00432                         }
00433                 }
00434 
00435                 // Remove the unwrap tags
00436                 $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
00437 
00438                 return $output; 
00439         }
00440 }
00441 
00442 
00443 /* End of file text_helper.php */
00444 /* Location: ./system/helpers/text_helper.php */