View Single Post
  #2  
Old 08-09-2006, 12:56 PM
valentino valentino is offline
OSP Starters
 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
valentino is on a distinguished road
Default

This function is pulled straight off the php.net site under the str_replace function, written by: dbergeron [at] clincab [dot] com

I used it a while go on a site for highlighting text, should be fine.

Code

<?php
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)) {
//this version bolds the text. you can replace the html tags with whatever you like.
$xtemp .= "<b>" . substr($x, $i , strlen($var)) . "</b>";
$i += strlen($var);
}
else {
$xtemp .= $x{$i};
$i++;
-- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
  Webmaster Forums - View Single Post - matching strings
View Single Post
  #2  
Old 08-09-2006, 12:56 PM
valentino valentino is offline
OSP Starters
 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
valentino is on a distinguished road
Default

This function is pulled straight off the php.net site under the str_replace function, written by: dbergeron [at] clincab [dot] com

I used it a while go on a site for highlighting text, should be fine.

Code

<?php
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)) {
//this version bolds the text. you can replace the html tags with whatever you like.
$xtemp .= "<b>" . substr($x, $i , strlen($var)) . "</b>";
$i += strlen($var);
}
else {
$xtemp .= $x{$i};
$i++;
-- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote