note 66185 added to function.preg-match

From: 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

« previous php.notes (#111403) next »