Re: Replacing non exact strings
| From: | Stuart Dallas | Date: | Fri, 03 May 2002 14:39:06 +0000 |
| Subject: | Re: Replacing non exact strings | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-95906@lists.php.net to get a copy of this message | ||
On 3 May 2002 at 11:36, Julio Nobrega Trabalhando wrote:
> $searched = "Joe Doe";
> $original = "Joe Cougar Doe";
> $final = "<b>Joe</b> Cougar <b>Doe</b>";
>
> Any ideas about how can I achieve this result? I am currently using
> eregi_replace($searched, "<b>$searched</b>", $original), so that's
> why
> it only accepts the whole string.
Split $searched using explode, then do the replace on each element of the resulting array...
$searched = "Joe Doe";
$original = "Joe Cougar Doe";
$searchedwords = explode(" ", $searched);
$final = $original;
foreach ($searchedwords as $word)
{
$final = eregi_replace($word, "<b>$word</b>", $final);
}
$final = "<b>Joe</b> Cougar <b>Doe</b>";
--
Stuart