NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP STRING QUESTION

Dan Cech dcech at phpwerx.net
Tue Oct 19 14:34:10 EDT 2004


Henry Ponce wrote:
> Hi:
> 
> This only helps if you use PHP 5.
> 
> Is there a way to do what was asked with PHP 4??
> 
> Thanx.

There is a way to do this in php4 for those who don't have/want to use 
preg_replace:

function str_ireplace ($find,$replace,$string)
{
   if (!is_array($find)) {
     $find = array($find);
   }

   if (!is_array($replace)) {
     // this will duplicate the string into an array the size of $find
     $rString = $replace;
     $replace = array();
     foreach ($find as $fKey => $fItem) {
       $replace[$fKey] = $rString;
     }
   }

   foreach ($find as $fKey => $fItem) {
     $between = explode(strtolower($fItem),strtolower($string));
     $pos = 0;

     foreach ($between as $bKey => $bItem) {
       $between[$bKey] = substr($string,$pos,strlen($bItem));
       $pos += strlen($bItem) + strlen($fItem);
     }

     if (isset($replace[$fKey])) {
       $string = implode($replace[$fKey],$between);
     } else {
       $string = implode('',$between);
     }
   }

   return($string);
}

the easiest way is to use preg_replace though:

$highlighted = preg_replace('/('.preg_quote($searched_for).')/i', '<span 
class="highlight">$1</span>', $searched_in);

With preg you can do even more fun things like pulling out context 
around matching terms, etc, for example:

function hilight_body($searched,$found)
{
   $newsearch = preg_replace('/\s+/is','|',preg_quote($searched,'/'));
   $found = htmlentities($found);

   $find = array(
     '/\$/is',                                              // remove 
dollar signs
     '/(('.$newsearch.')+)/is',                             // hilight
     '/(^|^[^<]*?\s)([^<]{0,100}<span)/ise',                // trim front
     '/(<\/span>[^<]{0,100})($|\s[^<]*?$)/ise',             // trim back
     '/(<\/span>[^<]{0,25})(\s[^<]*?)?(\s[^\s]*<span)/ise', // trim between
     '/###DOLLAR###/is'                                     // replace 
dollar signs
   );

   $replace = array(
     '###DOLLAR###',
     '<span class="highlight">$1</span>',
     '(trim("$1")==""?"$1":"... ")."$2"',
     '"$1".(trim("$2")=="$2"?"$2":" ...")',
     '"$1".(trim("$2")=="$2"?"$2":" ...")."$3"',
     '$'
   );

   $newfound = preg_replace($find,$replace,$found);

   if ($newfound != $found) {
     return $newfound;
   }

   $find = array(
     '/\$/is',                                              // remove 
dollar signs
     '/^(.{0,100})($|\s.*?$)/ise',                          // trim to 
length
     '/###DOLLAR###/is'                                     // replace 
dollar signs
   );

   $replace = array(
     '###DOLLAR###',
     '"$1".(trim("$2")==""?"$2":" ...")',
     '$'
   );

   return preg_replace($find,$replace,$found);
}

Which will return 100 characters before the first match, 25 characters 
after each match and 100 characters after the last match with the 
matched terms surrounded by span tags, or the first 100 characters of a 
string with no matches.

Dan

> On Tuesday 19 October 2004 14:57, Matt Juszczak wrote:
> 
>>http://us2.php.net/manual/en/function.str-ireplace.php
>>
>>Hope that helps,
>>
>>Matt
>>
>>SALMAN MAZHAR wrote:
>>
>>
>>>Hello everyone,
>>>
>>>My name is Salman Mazhar. I have a script that searches for a keyword 
>>>in a text file. I am able to search the word using the stristr() 
>>>function in php. This function is non-case sensitive. However, I want 
>>>to display the searched word in the line as highlighted in a different 
>>>color than the line. I use the str_replace() function and replace the 
>>>word with the word in red color but the str_replace() function is 
>>>case-sensitive. If I am searching for the word Computer and I type in 
>>>upper case C, its only going to replace those instances of computer 
>>>that begin with upper case C and leave the rest of them without any 
>>>color change. Does anyone know how to get around this problem?
>>>
>>>Thanks.
>>>Sincerely Yours,
>>>Salman Mazhar.



More information about the talk mailing list