note 66124 deleted from function.preg-match by bjori

From: Date: Mon, 15 May 2006 12:05:48 +0000
Subject: note 66124 deleted from function.preg-match by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-111317@lists.php.net to get a copy of this message
Note Submitter: ickata at ickata dot net ---- When you want to perform a case-insensitive match, you use the following code to do it: <?php preg_match ("/word/i",$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 = "ТеКст"; if (preg_match("/".createFilter($word)."/",$string)) echo "A match was found."; else echo "A match was NOT found!"; ?> And something else - if you want to perform a match for a whole word, do not use "/\bword\b/", better that: <?php preg_match ("/[^A-Za-zА-Яа-я]".createFilter("word") ."[^A-Za-zА-Яа-я]/", $text); ?> Hope this will help someone!

« previous php.notes (#111317) next »