note 66185 deleted from function.preg-match by colder
| From: | colder@php.net | Date: | Mon, 18 Jun 2007 18:29:48 +0000 |
| Subject: | note 66185 deleted from function.preg-match by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127624@lists.php.net to get a copy of this message | ||
Note Submitter: ickata at ickata dot net
----
If you want to perform a case-insensitive match with
cyrillic characters, there is a problem if your server is
Linux-based. So here is a useful function to perform
a case-insensitive match:
<?php
// define some functions which will transform a string
// to lower- or uppercases, because strtolower() and
// strtoupper do not work with cyrillic characters:
function cstrtolower($str) {
return strtr($str,
"ÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐРСТУФХЦЧШЩЪЬЮЯ",
"абвгдежзийклмнопÑÑÑÑÑÑ
ÑÑÑÑÑÑÑÑ");
}
function cstrtoupper($str) {
return strtr($str,
"абвгдежзийклмнопÑÑÑÑÑÑ
ÑÑÑÑÑÑÑÑ",
"ÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐРСТУФХЦЧШЩЪЬЮЯ");
}
// define a function which will create our filter -we
// need to break apart the characters of the keyword
// into groups, containing the lowercase and the
// uppercase of each character:
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;
}
// Example:
$string = "Това е ТеÑТ - пÑовеÑка";
$keyword = "ÑеÑÑ";
if (preg_match ("/".createFilter($word)."/", $text))
echo "A match was found.";
else echo "A match was NOT found!";
?>
And one more thing - if you want to perform a match
with a whole word only, do not use "/\bkeyword\b/",
use this:
<?php
preg_match ("/[Ð-Яа-ÑA-Za-z]keyword[Ð-Яа-ÑA-Za-z]/");
?>