note 70230 added to function.str-replace

From: Date: Sat, 07 Oct 2006 12:58:40 +0000
Subject: note 70230 added to function.str-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-118189@lists.php.net to get a copy of this message
an enhanced version of Dmitry Fedotov's function that can handle arrays as input and output arguments ... <? $string = "test1 test2 test3 test4"; echo str_replace_once('test', 'testing', $string); // testing1 test2 test3 test4 $string = ".NET sucks, Java sucks, PHP sucks"; // that doesn't sound right ... lets fix it! echo str_replace_once("sucks", array("is stupid", "is ugly", "rules"), $string); // .NET is is stupid, Java is ugly, PHP rules function str_replace_once($search, $replace, $subject, &$offset = 0) { if (is_array($search)) { if (is_array($replace)) { foreach ($search as $x => $value) $subject = str_replace_once($value, $replace[$x], $subject, $offset); } else { foreach ($search as $value) $subject = str_replace_once($value, $replace, $subject, $offset); } } else { if (is_array($replace)) { foreach ($replace as $value) $subject = str_replace_once($search, $value, $subject, $offset); } else { $pos = strpos($subject, $search, $offset); if ($pos !== false) { $offset = $pos+strlen($search); $subject = substr($subject, 0, $pos) . $replace . substr($subject, $offset); } } } return $subject; } ?> ---- Server IP: 216.235.15.211 Probable Submitter: 206.188.94.196 ---- Manual Page -- http://www.php.net/manual/en/function.str-replace.php Edit -- https://master.php.net/note/edit/70230 Del: integrated -- https://master.php.net/note/delete/70230/integrated Del: useless -- https://master.php.net/note/delete/70230/useless Del: bad code -- https://master.php.net/note/delete/70230/bad+code Del: spam -- https://master.php.net/note/delete/70230/spam Del: non-english -- https://master.php.net/note/delete/70230/non-english Del: in docs -- https://master.php.net/note/delete/70230/in+docs Del: other reasons-- https://master.php.net/note/delete/70230 Reject -- https://master.php.net/note/reject/70230 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#118189) next »