note 66125 deleted from function.preg-replace by bjori

From: Date: Mon, 15 May 2006 12:07:00 +0000
Subject: note 66125 deleted from function.preg-replace by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-111318@lists.php.net to get a copy of this message
Note Submitter: ickata at ickata dot net Reason: bad code ---- When you want to perform a case-insensitive replace, you use the following code to do it: <?php preg_replace("/word/i","<b>word</b>",$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 = "текст"; $newstring = preg_replace("/".createFilter($word)."/", "<b>\\0</b>",$string)); echo $newstring; ?> Hope this will help someone!

« previous php.notes (#111318) next »