Justtechjobs.com Find a programming school near you






Online Campus Both


php-general | 2000071

Re: [PHP] Using regex to replace text with bold version - problem with case From: Andreas Pour (pour <email protected>)
Date: 07/12/00

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 <email protected>}
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe <email protected>
> For additional commands, e-mail: php-general-help <email protected>
> To contact the list administrators, e-mail: php-list-admin <email protected>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe <email protected>
For additional commands, e-mail: php-general-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>