note 66185 added to function.preg-match
| From: | ickata at ickata dot net | Date: | Tue, 16 May 2006 19:23:34 +0000 |
| Subject: | note 66185 added to function.preg-match | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-111403@lists.php.net to get a copy of this message | ||
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]/");
?>
----
Server IP: 195.149.248.161
Probable Submitter: 62.204.128.40
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.preg-match.php
Edit -- http://master.php.net/note/edit/66185
Del: integrated -- http://master.php.net/note/delete/66185/integrated
Del: useless -- http://master.php.net/note/delete/66185/useless
Del: bad code -- http://master.php.net/note/delete/66185/bad+code
Del: spam -- http://master.php.net/note/delete/66185/spam
Del: non-english -- http://master.php.net/note/delete/66185/non-english
Del: in docs -- http://master.php.net/note/delete/66185/in+docs
Del: other reasons-- http://master.php.net/note/delete/66185
Reject -- http://master.php.net/note/reject/66185
Search -- http://master.php.net/manage/user-notes.php