note 89896 deleted from function.str-ireplace by danbrown

From: Date: Mon, 18 May 2009 14:44:42 +0000
Subject: note 89896 deleted from function.str-ireplace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155675@lists.php.net to get a copy of this message
Note Submitter: m dot kaczanowski at alianet dot pl ---- User "a dot lyskawa at pp-layouts dot com" made a few mistakes in mb_str_ireplace() function. First of all there is no such function like ok(). I guess it should look like: <? function ok($var) { return ($var!==false); } ?> There is no predefined constans like 'u8', str_ireplace trades count parameter as count of replacements and in function is unused variable $rc. I think that most of these mistakes was made wne function was taken from the users' "MB Class". I made some improvements too. Most of all we need str_ireplace to prevent case of replecement according to case of found search. So better mb_str_ireplace function should look like: <? function mb_str_ireplace($search, $replace, $subject, &$count = null, $encoding = 'UTF8') { $count = 0; $l1 = mb_strlen($search, $encoding); $l2 = mb_strlen($replace, $encoding); $rc = 0; $offset = 0; while(ok($p = mb_stripos($subject, $search, $offset, $encoding))) { if (ok($p)) { $found = mb_substr($subject, $p, $l1, $encoding); $offset2 = 0; while (ok($pp = mb_stripos($replace, $found, $offset2, $encoding))) { for ($i=0; $i<mb_strlen($found, $encoding); $i++) { $ls = mb_substr($found, $i, 1, $encoding); $lr = mb_substr($replace, $pp+$i, 1, $encoding); $lr = (ctype_lower($ls)?mb_strtolower($lr):mb_strtoupper($lr)); $replace = mb_substr($replace, 0, $pp+$i, $encoding) . $lr . mb_substr($replace, $pp+$i+1, mb_strlen($replace, $encoding), $encoding); } $offset2 = $pp + mb_strlen($found, $encoding);; } $subject = mb_substr($subject, 0, $p, $encoding) . $replace . mb_substr($subject, $p + $l1, mb_strlen($subject, $encoding), $encoding); } $offset = $p + $l2; $rc++; $count++; } return $subject; } ?>

« previous php.notes (#155675) next »