3 * @version $Id: str_ireplace.php,v 1.1 2006/02/25 13:50:17 harryf Exp $
8 //---------------------------------------------------------------
10 * UTF-8 aware alternative to str_ireplace
11 * Case-insensitive version of str_replace
12 * Note: requires utf8_strtolower
13 * Note: it's not fast and gets slower if $search / $replace is array
14 * Notes: it's based on the assumption that the lower and uppercase
15 * versions of a UTF-8 character will have the same length in bytes
16 * which is currently true given the hash table to strtolower
19 * @see http://www.php.net/str_ireplace
20 * @see utf8_strtolower
24 function utf8_ireplace($search, $replace, $str, $count = NULL){
26 if ( !is_array($search) ) {
28 $slen = strlen($search);
33 $search = utf8_strtolower($search);
35 $search = preg_quote($search);
36 $lstr = utf8_strtolower($str);
39 while ( preg_match('/(.*)'.$search.'/Us',$lstr, $matches) ) {
40 if ( $i === $count ) {
43 $mlen = strlen($matches[0]);
44 $lstr = substr($lstr, $mlen);
45 $str = substr_replace($str, $replace, $matched+
strlen($matches[1]), $slen);
53 foreach ( array_keys($search) as $k ) {
55 if ( is_array($replace) ) {
57 if ( array_key_exists($k,$replace) ) {
59 $str = utf8_ireplace($search[$k], $replace[$k], $str, $count);
63 $str = utf8_ireplace($search[$k], '', $str, $count);
69 $str = utf8_ireplace($search[$k], $replace, $str, $count);