Zikula_Core
[ class tree: Zikula_Core ] [ index: Zikula_Core ] [ all elements ]

Source for file StringUtil.class.php

Documentation is available at StringUtil.class.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright Robert Gasch
  6.  * @link http://www.zikula.org
  7.  * @version $Id: StringUtil.class.php 24342 2008-06-06 12:03:14Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @author Robert Gasch rgasch@gmail.com
  10.  * @package Zikula_Core
  11.  * @subpackage StringUtil
  12.  */
  13.  
  14. /**
  15.  * StringUtil
  16.  *
  17.  * @package Zikula_Core
  18.  * @subpackage StringUtil
  19.  */
  20. class StringUtil
  21. {
  22.     /**
  23.      * Count the instances of needle in the given string
  24.      *
  25.      * Why is this function here? PHP has a builtin substr_count()
  26.      * to do the same.
  27.      *
  28.      * @param haystack  the string to search
  29.      * @param needle    the needle to search for and count
  30.      *
  31.      * @return The numer of instances of needle in string
  32.      */
  33.     function countInstances ($haystack$needle)
  34.     {
  35.         return substr_count($haystack$needle);
  36.     }
  37.  
  38.  
  39.     /**
  40.      * Truncate a string to a certain length
  41.      *
  42.      * @param string       the string to operate on
  43.      * @param limit        the maximum number of characters displayed (optional) (default=80)
  44.      * @param appendDots   whether or not to append '...' to the maximum number of characters displayed (optional) (default=80)
  45.      *
  46.      * @return The potentially truncated string
  47.      */
  48.     function getTruncatedString ($string$limit=80$appendDots=true)
  49.     {
  50.         $len strlen($string);
  51.  
  52.         if ($len $limit)
  53.         {
  54.             $string substr($string0$limit);
  55.  
  56.             if ($appendDots)
  57.                 $string .= '...';
  58.         }
  59.  
  60.         return $string;
  61.     }
  62.  
  63.  
  64.     /**
  65.      * Translate html input newlines to <br /> sequences.
  66.      * This function is necessary as inputted strings will contain
  67.      * "\n\r" instead of just "\n"
  68.      *
  69.      * @param string    the string to operate on
  70.      *
  71.      * @return The converted string
  72.      */
  73.     function nl2html ($string)
  74.     {
  75.         $str str_replace("\n"'<br />'$string);
  76.         $str str_replace("\r"''$str);
  77.  
  78.         return $str;
  79.     }
  80.  
  81.  
  82.     /**
  83.      * Tokenize a string according to the given parameters.
  84.      * This function just wraps explode to provide a more java-similar syntax
  85.      *
  86.      * @param string     the string to tokenize
  87.      * @param delimeter  the delimeter to use
  88.      * @param max        the maximal number of tokens to generate (optional) (default=999999)
  89.      *
  90.      * @return The token array
  91.      */
  92.     function tokenize ($string$delimeter$max=999999)
  93.     {
  94.         return explode($delimeter$string$max);
  95.     }
  96.  
  97.  
  98.     /**
  99.      * Case-Insensitive version of strpos (standard only available in PHP 5)
  100.      *
  101.      * @param haystack  the string to search
  102.      * @param needle    the string to search for
  103.      * @param offset    the search start offset position (optional) (default=0)
  104.      *
  105.      * @return The token array
  106.      */
  107.     //if (!function_exists("stripos")) {
  108.         function stripos ($haystack$needle$offset=0)
  109.         {
  110.             return strpos (strtoupper($haystack)strtoupper($needle)$offset);
  111.         }
  112.     //}
  113.  
  114.  
  115.     /**
  116.      * Returns the left x chars of a string. If the string is longer than x,
  117.      * the whole string is returned
  118.      *
  119.      * @param string       the string to operate on
  120.      * @param left         the number of chars to return
  121.      *
  122.      * @return part of the supplied string
  123.      */
  124.     function left ($string$left=0)
  125.     {
  126.         $len strlen($string);
  127.         if ($len $left{
  128.             $string substr($string0$left);
  129.         }
  130.         return $string;
  131.     }
  132.  
  133.     /**
  134.      * Returns the right x chars of a string. If the string is longer than x,
  135.      * the whole string is returned
  136.      *
  137.      * @param string       the string to operate on
  138.      * @param right        the number of chars to return
  139.      *
  140.      * @return part of the supplied string
  141.      */
  142.     function right ($string$right=0)
  143.     {
  144.         $len strlen($string);
  145.         if ($len $right{
  146.             $string substr($string$len $right$right);
  147.         }
  148.         return $string;
  149.     }
  150.  
  151.  
  152.     function highlightWords($text$wordStr$contextSize=200)
  153.     {
  154.         $colors array('#FFFF00','#00FFFF','#99FF99','#FF9999','#FF66FF',
  155.                         '#880000','#00AA00','#886800','#004699','#990099');
  156.  
  157.         // Strip HTML tags and special chars completely
  158.         $text strip_tags($text);
  159.         $text html_entity_decode($text);
  160.  
  161.         // Split words into word array
  162.         $words preg_split('/ /'$wordStr-1PREG_SPLIT_NO_EMPTY);
  163.  
  164.         $firstMatch true;
  165.  
  166.         $ranges array();
  167.         $textLength strlen($text);
  168.         $lowerText strtolower($text);
  169.  
  170.         $i 1;
  171.         $pattern array();
  172.         $replacements array();
  173.         foreach ($words as $word)
  174.         {
  175.             $patterns["/($word)/i";
  176.             $replacements['<b class="highlight' ($i 10"\">\\1</b>";
  177.             ++$i;
  178.         }
  179.  
  180.         $completeReplacedText preg_replace($patterns$replacements$text);
  181.  
  182.         // Find first position of first "<" (which is the first highlighted word)
  183.         $startPos strpos($completeReplacedText"<");
  184.         if ($startPos === false)
  185.             $startPos 0;
  186.         $startPos max(0$startPos-$contextSize/2);
  187.         while ($startPos 0  &&  $completeReplacedText[$startPos!= ' ')
  188.             --$startPos;
  189.  
  190.         // Find last position of ">" (last highlighted word).
  191.         $endPos strrpos($completeReplacedText">");
  192.         if ($endPos === false)
  193.             $endPos $contextSize;
  194.         $endPos min(strlen($completeReplacedText)$endPos+$contextSize/2);
  195.         while ($endPos strlen($completeReplacedText)-1  &&  $completeReplacedText[$endPos!= ' ')
  196.             ++$endPos;
  197.  
  198.         return substr($completeReplacedText$startPos$endPos-$startPos);
  199.     }
  200. }

Documentation generated on Fri, 18 Jul 2008 21:58:23 +0200 by phpDocumentor 1.4.1