note 102845 modified in function.str-ireplace by danbrown
| From: | danbrown@php.net | Date: | Wed, 09 Mar 2011 21:40:13 +0000 |
| Subject: | note 102845 modified in function.str-ireplace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177727@lists.php.net to get a copy of this message | ||
Warning with highlighting ...
I used :
<?php
$text = preg_replace('/('.$q.')/i','<span class=highlighting
"">$1</span>' , $text);
?>
Because this line do not allow to highlight uppercase and lowercase correctly (transform uppercase
to lowercase for exemple)
<?php
$text = str_ireplace( $q , '<span class=highlighting
"">'.$q.'</span>', $text);
?>
But when $q contain some regex you have some problems ... for exemple :
<?php $q = '('; ?>
So you must use preg_replace to highlight correctly the text and you must create a function for
escape bad regex caracters !
I think that a better function can be found but this works I guess :
<?php
function regex_escape( $q )
{
return preg_replace('/([\[\]\(\)\{\}\-\.\*\?\|\^\$])/', '\$1', $q);
}
?>
--was--
Warning with highlighting ...
I used :
$text = preg_replace('/('.$q.')/i','<span class=highlighting
"">$1</span>' , $text);
Because this line do not allow to highlight uppercase and lowercase correctly (transform uppercase
to lowercase for exemple)
$text = str_ireplace( $q , '<span class=highlighting
"">'.$q.'</span>', $text);
But when $q contain some regex you have some problems ... for exemple :
$q = '(';
So you must use preg_replace to highlight correctly the text and you must create a function for
escape bad regex caracters !
I think that a better function can be found but this works I guess :
function regex_escape( $q )
{
return preg_replace('/([\[\]\(\)\{\}\-\.\*\?\|\^\$])/', '\$1', $q);
}
http://php.net/manual/en/function.str-ireplace.php