Source for file StringUtil.class.php
Documentation is available at StringUtil.class.php
* Zikula Application Framework
* @copyright Robert Gasch
* @link http://www.zikula.org
* @version $Id: StringUtil.class.php 24342 2008-06-06 12:03:14Z markwest $
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
* @author Robert Gasch rgasch@gmail.com
* Count the instances of needle in the given string
* Why is this function here? PHP has a builtin substr_count()
* @param haystack the string to search
* @param needle the needle to search for and count
* @return The numer of instances of needle in string
* Truncate a string to a certain length
* @param string the string to operate on
* @param limit the maximum number of characters displayed (optional) (default=80)
* @param appendDots whether or not to append '...' to the maximum number of characters displayed (optional) (default=80)
* @return The potentially truncated string
$string = substr($string, 0, $limit);
* Translate html input newlines to <br /> sequences.
* This function is necessary as inputted strings will contain
* "\n\r" instead of just "\n"
* @param string the string to operate on
* @return The converted string
* Tokenize a string according to the given parameters.
* This function just wraps explode to provide a more java-similar syntax
* @param string the string to tokenize
* @param delimeter the delimeter to use
* @param max the maximal number of tokens to generate (optional) (default=999999)
* @return The token array
function tokenize ($string, $delimeter, $max= 999999)
return explode($delimeter, $string, $max);
* Case-Insensitive version of strpos (standard only available in PHP 5)
* @param haystack the string to search
* @param needle the string to search for
* @param offset the search start offset position (optional) (default=0)
* @return The token array
//if (!function_exists("stripos")) {
function stripos ($haystack, $needle, $offset= 0)
* Returns the left x chars of a string. If the string is longer than x,
* the whole string is returned
* @param string the string to operate on
* @param left the number of chars to return
* @return a part of the supplied string
function left ($string, $left= 0)
$string = substr($string, 0, $left);
* Returns the right x chars of a string. If the string is longer than x,
* the whole string is returned
* @param string the string to operate on
* @param right the number of chars to return
* @return a part of the supplied string
function right ($string, $right= 0)
$string = substr($string, $len - $right, $right);
$colors = array('#FFFF00','#00FFFF','#99FF99','#FF9999','#FF66FF',
'#880000','#00AA00','#886800','#004699','#990099');
// Strip HTML tags and special chars completely
// Split words into word array
$words = preg_split('/ /', $wordStr, - 1, PREG_SPLIT_NO_EMPTY);
foreach ($words as $word)
$patterns[] = "/($word)/i";
$replacements[] = '<b class="highlight' . ($i % 10) . "\">\\1</b>";
$completeReplacedText = preg_replace($patterns, $replacements, $text);
// Find first position of first "<" (which is the first highlighted word)
$startPos = strpos($completeReplacedText, "<");
$startPos = max(0, $startPos- $contextSize/ 2);
while ($startPos > 0 && $completeReplacedText[$startPos] != ' ')
// Find last position of ">" (last highlighted word).
$endPos = strrpos($completeReplacedText, ">");
$endPos = min(strlen($completeReplacedText), $endPos+ $contextSize/ 2);
while ($endPos < strlen($completeReplacedText)- 1 && $completeReplacedText[$endPos] != ' ')
return substr($completeReplacedText, $startPos, $endPos- $startPos);
|