note 33425 added to function.str-replace
| From: | dbergeron at rack1 dot php dot net | Date: | Wed, 25 Jun 2003 16:08:56 +0000 |
| Subject: | note 33425 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-50998@lists.php.net to get a copy of this message | ||
here is a function to highlight a part of a string. Unlike str_replace, it is both case insensitive,
and maintains the case of the highlighted text.
function highlight($x,$var) {//$x is the string, $var is the text to be highlighted
if ($var != "") {
$xtemp = "";
$i=0;
while($i<strlen($x)){
if((($i + strlen($var)) <= strlen($x)) && (strcasecmp($var, substr($x, $i,
strlen($var))) == 0)) {
$xtemp .= "<b>" . substr($x, $i , strlen($var)) . "</b>";//this
version bolds the text. you can replace the html tags with whatever you like.
$i += strlen($var);
}
else {
$xtemp .= $x{$i};
$i++;
}
}
$x = $xtemp;
}
return $x;
}
Example:
$string = "AaBbCcDd";
$string = highlight($string, "bc");
echo $string; //AaB<b>bC</b>cDd
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33425
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33425&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33425&report=yes