note 66185 deleted from function.preg-match by colder

From: 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]/"); ?>

« previous php.notes (#127624) next »