Using regex to replace text with bold version - problem with case
| From: | Steven Werby | Date: | Wed, 12 Jul 2000 19:52:46 +0000 |
| Subject: | Using regex to replace text with bold version - problem with case | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-6242@lists.php.net to get a copy of this message | ||
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}