note 66125 added to function.preg-replace
| From: | ickata at ickata dot net | Date: | Mon, 15 May 2006 11:57:45 +0000 |
| Subject: | note 66125 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-111315@lists.php.net to get a copy of this message | ||
When you want to perform a case-insensitive replace,
you use the following code to do it:
<?php
preg_replace("/word/i","<b>word</b>",$string);
?>
However, there is a problem if you use cyrillic characters
and your host server is Linux - no matter that you use
the "i" parameter after the filter. This is really bad for
Russians and Bulgarians, but there is a solution:
<?php
// Firstly, we need to define some functions which will convert
// our cyrillic string to uppercase or lowercase - strtolower()
// and strtoupper() do not work if your string contains cyrillic
// characters and your server is Linux.
function cstrtolower($str) {
return strtr($str,
"ÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐРСТУФХЦЧШЩЪЬЮЯ",
"абвгдежзийклмнопÑÑÑÑÑÑ
ÑÑÑÑÑÑÑÑ");
}
function cstrtoupper($str) {
return strtr($str,
"абвгдежзийклмнопÑÑÑÑÑÑ
ÑÑÑÑÑÑÑÑ",
"ÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐРСТУФХЦЧШЩЪЬЮЯ");
}
// Now we have to define a function which will break apart
// the word for which we are searching in the string in
// groups - lowercase and uppercase in a group, for example
// if we are searching for "word", our filter must looks like
// this: "/[Ww][Oo][Rr][Dd]/"
function createFilter ($string) {
$string = cstrtolower($string);
for ($i=0;$i<strlen($string);$i++) {
$letter_small = substr($string,$i,1);
$letter_big = cstrtoupper($letter_small);
$newstr .= â[â.$letter_small.$letter_big.â]â;
}
return $newstr;
}
// And we are ready to perform the match:
$string = "Това е ТекÑТ на
киÑилиÑа";
$word = "ÑекÑÑ";
$newstring = preg_replace("/".createFilter($word)."/",
"<b>\\0</b>",$string));
echo $newstring;
?>
Hope this will help someone!
----
Server IP: 195.149.248.161
Probable Submitter: 85.187.161.147
----
X-Spam-Status: No, hits=3.7 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_70_80,HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- http://master.php.net/note/edit/66125
Del: integrated -- http://master.php.net/note/delete/66125/integrated
Del: useless -- http://master.php.net/note/delete/66125/useless
Del: bad code -- http://master.php.net/note/delete/66125/bad+code
Del: spam -- http://master.php.net/note/delete/66125/spam
Del: non-english -- http://master.php.net/note/delete/66125/non-english
Del: in docs -- http://master.php.net/note/delete/66125/in+docs
Del: other reasons-- http://master.php.net/note/delete/66125
Reject -- http://master.php.net/note/reject/66125
Search -- http://master.php.net/manage/user-notes.php