Re: Battling with highlighting search criteria
| From: | Michael Sims | Date: | Tue, 05 Nov 2002 03:09:54 +0000 |
| Subject: | Re: Battling with highlighting search criteria | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-122724@lists.php.net to get a copy of this message | ||
On Mon, 4 Nov 2002 11:59:20 +0200, you wrote:
>Ok, this works.
>
>Just one more thing,
>
>How can I get the return in the same case as it was originally?
Use backreferences to capture what matched and use that in the replace
string. Example:
<?
$search = "is";
$str = "This is a test, isn't it?";
$str = preg_replace("/($search)/i", "<font
color=\"red\">$1</font>",
$str);
echo $str;
echo "<br>";
$str = "Is this also a test? Yes, it is!";
$str = preg_replace("/($search)/i", "<font
color=\"red\">$1</font>",
$str);
echo $str;
echo "<br>";
?>