Source for file text_functions.php

Documentation is available at text_functions.php

  1. <?php
  2. /**
  3.  * CodeIgniter
  4.  *
  5.  * An open source application development framework for PHP 4.3.2 or newer
  6.  *
  7.  * @package        CodeIgniter
  8.  * @subpackage     Helpers
  9.  * @author        ExpressionEngine Dev Team
  10.  * @copyright    Copyright (c) 2006, EllisLab, Inc.
  11.  * @license        http://codeigniter.com/user_guide/license.html
  12.  * @link        http://codeigniter.com
  13.  * @since        Version 1.0
  14.  * @filesource
  15.  */
  16.  
  17. // ------------------------------------------------------------------------
  18.  
  19. /**
  20.  * CodeIgniter Text Helpers
  21.  *
  22.  * @author        ExpressionEngine Dev Team
  23.  * @link        http://codeigniter.com/user_guide/helpers/text_helper.html
  24.  */
  25.  
  26. // ------------------------------------------------------------------------
  27.  
  28. if (function_exists('word_limiter')) {
  29.     /**
  30.      * Word Limiter
  31.      *
  32.      * Limits a string to X number of words.
  33.      *
  34.      * @access    public
  35.      * @param    string 
  36.      * @param    integer 
  37.      * @param    string    the end character. Usually an ellipsis
  38.      * @return    string 
  39.      */    
  40.     function word_limiter($str$limit 100$end_char '&#8230;')
  41.     {
  42.         if (trim($str== '')
  43.         {
  44.             return $str;
  45.         }
  46.     
  47.         preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/'$str$matches);
  48.             
  49.         if (strlen($str== strlen($matches[0]))
  50.         {
  51.             $end_char '';
  52.         }
  53.         
  54.         return rtrim($matches[0]).$end_char;
  55.     }
  56. }
  57.     
  58. // ------------------------------------------------------------------------
  59.  
  60. if (function_exists('character_limiter')) {
  61.     /**
  62.      * Character Limiter
  63.      *
  64.      * Limits the string based on the character count.  Preserves complete words
  65.      * so the character count may not be exactly as specified.
  66.      *
  67.      * @access    public
  68.      * @param    string 
  69.      * @param    integer 
  70.      * @param    string    the end character. Usually an ellipsis
  71.      * @return    string 
  72.      */    
  73.     function character_limiter($str$n 500$end_char '&#8230;')
  74.     {
  75.         if (strlen($str$n)
  76.         {
  77.             return $str;
  78.         }
  79.         
  80.         $str preg_replace("/\s+/"' 'preg_replace("/(\r\n|\r|\n)/"" "$str));
  81.  
  82.         if (strlen($str<= $n)
  83.         {
  84.             return $str;
  85.         }
  86.                                     
  87.         $out "";
  88.         foreach (explode(' 'trim($str)) as $val)
  89.         {
  90.             if (strlen($out.$val.' '>= $n)
  91.             {
  92.                 return trim($out).$end_char;
  93.             }        
  94.             $out .= $val.' ';
  95.         }
  96.     }
  97. }
  98.     
  99. // ------------------------------------------------------------------------
  100.  
  101. if (function_exists('ascii_to_entities')) {
  102.     /**
  103.      * High ASCII to Entities
  104.      *
  105.      * Converts High ascii text and MS Word special characters to character entities
  106.      *
  107.      * @access    public
  108.      * @param    string 
  109.      * @return    string 
  110.      */    
  111.     function ascii_to_entities($str)
  112.     {
  113.        $count    1;
  114.        $out    '';
  115.        $temp    array();
  116.     
  117.        for ($i 0$s strlen($str)$i $s$i++)
  118.        {
  119.            $ordinal ord($str[$i]);
  120.     
  121.            if ($ordinal 128)
  122.            {
  123.                $out .= $str[$i];
  124.            }
  125.            else
  126.            {
  127.                if (count($temp== 0)
  128.                {
  129.                    $count ($ordinal 2243;
  130.                }
  131.         
  132.                $temp[$ordinal;
  133.         
  134.                if (count($temp== $count)
  135.                {
  136.                    $number ($count == 3(($temp['0'164096(($temp['1'6464($temp['2'64(($temp['0'3264($temp['1'64);
  137.  
  138.                    $out .= '&#'.$number.';';
  139.                    $count 1;
  140.                    $temp array();
  141.                }
  142.            }
  143.        }
  144.  
  145.        return $out;
  146.     }
  147. }
  148.     
  149. // ------------------------------------------------------------------------
  150.  
  151. if (function_exists('entities_to_ascii')) {
  152.     /**
  153.      * Entities to ASCII
  154.      *
  155.      * Converts character entities back to ASCII
  156.      *
  157.      * @access    public
  158.      * @param    string 
  159.      * @param    bool 
  160.      * @return    string 
  161.      */    
  162.     function entities_to_ascii($str$all TRUE)
  163.     {
  164.        if (preg_match_all('/\&#(\d+)\;/'$str$matches))
  165.        {
  166.            for ($i 0$s count($matches['0'])$i $s$i++)
  167.            {                
  168.                $digits $matches['1'][$i];
  169.  
  170.                $out '';
  171.  
  172.                if ($digits 128)
  173.                {
  174.                    $out .= chr($digits);
  175.         
  176.                }
  177.                elseif ($digits 2048)
  178.                {
  179.                    $out .= chr(192 (($digits ($digits 64)) 64));
  180.                    $out .= chr(128 ($digits 64));
  181.                }
  182.                else
  183.                {
  184.                    $out .= chr(224 (($digits ($digits 4096)) 4096));
  185.                    $out .= chr(128 ((($digits 4096($digits 64)) 64));
  186.                    $out .= chr(128 ($digits 64));
  187.                }
  188.  
  189.                $str str_replace($matches['0'][$i]$out$str);                
  190.            }
  191.        }
  192.  
  193.        if ($all)
  194.        {
  195.            $str str_replace(array("&amp;""&lt;""&gt;""&quot;""&apos;""&#45;"),
  196.                               array("&","<",">","\"""'""-"),
  197.                               $str);
  198.        }
  199.  
  200.        return $str;
  201.     }
  202. }
  203.     
  204. // ------------------------------------------------------------------------
  205.  
  206. if (function_exists('word_censor')) {
  207.     /**
  208.      * Word Censoring Function
  209.      *
  210.      * Supply a string and an array of disallowed words and any
  211.      * matched words will be converted to #### or to the replacement
  212.      * word you've submitted.
  213.      *
  214.      * @access    public
  215.      * @param    string    the text string
  216.      * @param    string    the array of censoered words
  217.      * @param    string    the optional replacement value
  218.      * @return    string 
  219.      */    
  220.     function word_censor($str$censored$replacement '')
  221.     {
  222.         if is_array($censored))
  223.         {
  224.             return $str;
  225.         }
  226.  
  227.         $str ' '.$str.' ';
  228.         foreach ($censored as $badword)
  229.         {
  230.             if ($replacement != '')
  231.             {
  232.                 $str preg_replace("/\b(".str_replace('\*''\w*?'preg_quote($badword)).")\b/i"$replacement$str);
  233.             }
  234.             else
  235.             {
  236.                 $str preg_replace("/\b(".str_replace('\*''\w*?'preg_quote($badword)).")\b/ie""str_repeat('#', strlen('\\1'))"$str);
  237.             }
  238.         }
  239.     
  240.         return trim($str);
  241.     }
  242. }
  243.     
  244. // ------------------------------------------------------------------------
  245.  
  246. if (function_exists('highlight_code')) {
  247.     /**
  248.      * Code Highlighter
  249.      *
  250.      * Colorizes code strings
  251.      *
  252.      * @access    public
  253.      * @param    string    the text string
  254.      * @return    string 
  255.      */    
  256.     function highlight_code($str)
  257.     {        
  258.         // The highlight string function encodes and highlights
  259.         // brackets so we need them to start raw
  260.         $str str_replace(array('&lt;''&gt;')array('<''>')$str);
  261.     
  262.         // Replace any existing PHP tags to temporary markers so they don't accidentally
  263.         // break the string out of PHP, and thus, thwart the highlighting.
  264.     
  265.         $str str_replace(array('<?''?>''<%''%>''\\''</script>')
  266.                             array('phptagopen''phptagclose''asptagopen''asptagclose''backslashtmp''scriptclose')$str);
  267.  
  268.         // The highlight_string function requires that the text be surrounded
  269.         // by PHP tags.  Since we don't know if A) the submitted text has PHP tags,
  270.         // or B) whether the PHP tags enclose the entire string, we will add our
  271.         // own PHP tags around the string along with some markers to make replacement easier later
  272.     
  273.         $str '<?php tempstart'."\n".$str.'tempend ?>';
  274.     
  275.         // All the magic happens here, baby!
  276.         $str highlight_string($strTRUE);
  277.  
  278.         // Prior to PHP 5, the highlight function used icky font tags
  279.         // so we'll replace them with span tags.    
  280.         if (abs(phpversion()) 5)
  281.         {
  282.             $str str_replace(array('<font ''</font>')array('<span ''</span>')$str);
  283.             $str preg_replace('#color="(.*?)"#''style="color: \\1"'$str);
  284.         }
  285.     
  286.         // Remove our artificially added PHP
  287.         $str preg_replace("#\<code\>.+?tempstart\<br />(?:\</span\>)?#is""<code>\n"$str);
  288.         $str preg_replace("#tempend.+#is""</span>\n</code>"$str);    
  289.     
  290.         // Replace our markers back to PHP tags.
  291.         $str str_replace(array('phptagopen''phptagclose''asptagopen''asptagclose''backslashtmp''scriptclose'),
  292.                             array('&lt;?''?&gt;''&lt;%''%&gt;''\\''&lt;/script&gt;')$str);
  293.                                         
  294.         return $str;
  295.     }
  296. }
  297.     
  298. // ------------------------------------------------------------------------
  299.  
  300. if (function_exists('highlight_phrase')) {
  301.     /**
  302.      * Phrase Highlighter
  303.      *
  304.      * Highlights a phrase within a text string
  305.      *
  306.      * @access    public
  307.      * @param    string    the text string
  308.      * @param    string    the phrase you'd like to highlight
  309.      * @param    string    the openging tag to precede the phrase with
  310.      * @param    string    the closing tag to end the phrase with
  311.      * @return    string 
  312.      */    
  313.     function highlight_phrase($str$phrase$tag_open '<strong>'$tag_close '</strong>')
  314.     {
  315.         if ($str == '')
  316.         {
  317.             return '';
  318.         }
  319.     
  320.         if ($phrase != '')
  321.         {
  322.             return preg_replace('/('.preg_quote($phrase'/').')/i'$tag_open."\\1".$tag_close$str);
  323.         }
  324.  
  325.         return $str;
  326.     }
  327. }
  328.     
  329. // ------------------------------------------------------------------------
  330.  
  331. if (function_exists('word_wrap')) {
  332.     /**
  333.      * Word Wrap
  334.      *
  335.      * Wraps text at the specified character.  Maintains the integrity of words.
  336.      * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
  337.      * will URLs.
  338.      *
  339.      * @access    public
  340.      * @param    string    the text string
  341.      * @param    integer    the number of characters to wrap at
  342.      * @return    string 
  343.      */    
  344.     function word_wrap($str$charlim '76')
  345.     {
  346.         // Se the character limit
  347.         if is_numeric($charlim))
  348.             $charlim 76;
  349.     
  350.         // Reduce multiple spaces
  351.         $str preg_replace("| +|"" "$str);
  352.     
  353.         // Standardize newlines
  354.         $str preg_replace("/\r\n|\r/""\n"$str);
  355.     
  356.         // If the current word is surrounded by {unwrap} tags we'll 
  357.         // strip the entire chunk and replace it with a marker.
  358.         $unwrap array();
  359.         if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s"$str$matches))
  360.         {
  361.             for ($i 0$i count($matches['0'])$i++)
  362.             {
  363.                 $unwrap[$matches['1'][$i];                
  364.                 $str str_replace($matches['1'][$i]"{{unwrapped".$i."}}"$str);
  365.             }
  366.         }
  367.     
  368.         // Use PHP's native function to do the initial wordwrap.  
  369.         // We set the cut flag to FALSE so that any individual words that are 
  370.         // too long get left alone.  In the next step we'll deal with them.
  371.         $str wordwrap($str$charlim"\n"FALSE);
  372.     
  373.         // Split the string into individual lines of text and cycle through them
  374.         $output "";
  375.         foreach (explode("\n"$stras $line
  376.         {
  377.             // Is the line within the allowed character count?
  378.             // If so we'll join it to the output and continue
  379.             if (strlen($line<= $charlim)
  380.             {
  381.                 $output .= $line."\n";            
  382.                 continue;
  383.             }
  384.             
  385.             $temp '';
  386.             while((strlen($line)) $charlim
  387.             {
  388.                 // If the over-length word is a URL we won't wrap it
  389.                 if (preg_match("!\[url.+\]|://|wwww.!"$line))
  390.                 {
  391.                     break;
  392.                 }
  393.  
  394.                 // Trim the word down
  395.                 $temp .= substr($line0$charlim-1);
  396.                 $line substr($line$charlim-1);
  397.             }
  398.         
  399.             // If $temp contains data it means we had to split up an over-length 
  400.             // word into smaller chunks so we'll add it back to our current line
  401.             if ($temp != '')
  402.             {
  403.                 $output .= $temp "\n" $line
  404.             }
  405.             else
  406.             {
  407.                 $output .= $line;
  408.             }
  409.  
  410.             $output .= "\n";
  411.         }
  412.  
  413.         // Put our markers back
  414.         if (count($unwrap0)
  415.         {    
  416.             foreach ($unwrap as $key => $val)
  417.             {
  418.                 $output str_replace("{{unwrapped".$key."}}"$val$output);
  419.             }
  420.         }
  421.  
  422.         // Remove the unwrap tags
  423.         $output str_replace(array('{unwrap}''{/unwrap}')''$output);
  424.  
  425.         return $output;    
  426.     }
  427. }
  428.  
  429. if (function_exists('wordwrap_utf8'))
  430. {
  431.     /**
  432.      * Word Wrap UTF8
  433.      *
  434.      * Esta funcion es identica a la funcion PHP wordwrap pero permite hacer.
  435.      * los cortes en UTF8.
  436.      * 
  437.      * El parametro $cCorte es la cadena de corte que se pone al final de cada linea.
  438.      * El parametro $nCorte indica si se debe forzar el corte de linea aun cuando
  439.      * quede una cadena cortada por la mitad.
  440.      * 
  441.      * @access public
  442.      * @param string $cCadena 
  443.      * @param int $nLongitud 
  444.      * @param string $cCorte 
  445.      * @param int $nCorte 
  446.      * @return string 
  447.      */
  448.     function wordwrap_utf8($cCadena$nLongitud$cCorte='<br />'$nCorte=1)
  449.     {
  450.         return utf8_encode(wordwrap(utf8_decode($cCadena)$nLongitud$cCorte$nCorte));
  451.     }
  452. }
  453. ?>

Documentation generated on Tue, 22 Nov 2011 13:29:00 -0200 by phpDocumentor 1.4.3