note 89896 added to function.str-ireplace

From: Date: Fri, 27 Mar 2009 13:59:37 +0000
Subject: note 89896 added to function.str-ireplace
Groups: php.notes 
Request: Send a blank email to php-notes+get-152519@lists.php.net to get a copy of this message
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; } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 83.6.219.245 ---- Manual Page -- http://www.php.net/manual/en/function.str-ireplace.php Edit -- https://master.php.net/note/edit/89896 Del: integrated -- https://master.php.net/note/delete/89896/integrated Del: useless -- https://master.php.net/note/delete/89896/useless Del: bad code -- https://master.php.net/note/delete/89896/bad+code Del: spam -- https://master.php.net/note/delete/89896/spam Del: non-english -- https://master.php.net/note/delete/89896/non-english Del: in docs -- https://master.php.net/note/delete/89896/in+docs Del: other reasons-- https://master.php.net/note/delete/89896 Reject -- https://master.php.net/note/reject/89896 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#152519) next »