Re: Using regex to replace text with bold version - problem with case
| From: | Andreas Pour | Date: | Wed, 12 Jul 2000 20:38:35 +0000 |
| Subject: | Re: Using regex to replace text with bold version - problem with case | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-6258@lists.php.net to get a copy of this message | ||
Hi,
Use:
eregi_replace("($criteria)", "<b>\\1</b>", $txt);
Ciao,
Andreas
Steven Werby wrote:
>
> I have a function that searches a string for supplied criteria value.
> I have the following string:
>
> $str = "PHP is great. Isn't php great? I like Php.";
>
> If $criteria equals PHP, Php, php (or even PhP) I want to convert $str to:
>
> <b>PHP</b> is great. Isn't <b>php</b> great? I like
> <b>php</b>.
>
> I've tried:
>
> 1.
> $str = eregi_replace("$criteria", "<b>$criteria</b>", $txt)
> This converts all case-insensitive occurences of "php" to the case used in $criteria.
>
> 2.
> $str = ereg_replace("$criteria", "<b>$criteria</b>", $txt)
> This only matches case-sensitive matches.
>
> 3.
> $criteria_2 = strtolower($criteria);
> $txt = ereg_replace("$criteria_2", "<b>$criteria_2</b>", $txt);
> $criteria_3 = strtoupper(substr($criteria,0,1)) . substr($criteria,1);
> $txt = ereg_replace("$criteria_3", "<b>$criteria_3</b>", $txt);
>
> This works pretty well. It matches "php" and "Php", but doesn't match
> anything with
> upper-case characters anywhere other than the beginning of the word.
>
> Is there a better way to approach this? Does anyone have a solution that works?
>
> Steven Werby {steven-lists@befriend.com}
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net