note 98498 deleted from function.str-ireplace by danbrown
| From: | danbrown@php.net | Date: | Fri, 18 Jun 2010 19:47:53 +0000 |
| Subject: | note 98498 deleted from function.str-ireplace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170252@lists.php.net to get a copy of this message | ||
Note Submitter: tanner dot postert at gmail dot com
----
I've modified the search result search/replace below with this which takes the original term
and replaces it with the same term with <b>tags</b> wrapped around it. Also allows
multiple keywords by a space separated string, but could easily be adapted to handle an array.
function bold( $subject, $keywords ) {
// Replaces $findme in $subject with $replacewith
// Ignores the case and do keep the original capitalization by using $1 in $replacewith
// Required: PHP 5
$rest = $subject;
$result = '';
foreach ( explode( " ", $keywords ) as $findme ) {
while (stripos($rest, $findme) !== false) {
$pos = stripos($rest, $findme);
// Remove the wanted string from $rest and append it to $result
$result .= substr($rest, 0, $pos);
$rest = substr($rest, $pos, strlen($rest)-$pos);
// Remove the wanted string from $rest and place it correctly into $result
$result .= str_replace('$1', substr($rest, 0, strlen($findme)),
'<b>'.'$1'.'</b>');
$rest = substr($rest, strlen($findme), strlen($rest)-strlen($findme));
}
}
// After the last match, append the rest
$result .= $rest;
return $result;
}