note 84946 added to function.str-replace
| From: | max at efoxdesigns dot com | Date: | Wed, 06 Aug 2008 20:13:40 +0000 |
| Subject: | note 84946 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143242@lists.php.net to get a copy of this message | ||
I'm sorry. I can't believe it: I messed up again. Something that I forgot is that the
php empty() function considers the number zero (0) as an empty value. Therefore, we need to change
our if statement to the following:
<?php
if ($i[0] !== null) $subject .= $replace[$i[0]];
?>
If we left it how it was, the replace ignores the first element of the arrays. ($array[0])
The reason that I used !empty() before, is that I wasn't sure what array_keys() returns if the
element is not found (null? and empty string?), since both null and the empty string print out the
same and I couldn't find what it returned in this case, in the manual.
The final, working function would look like:
<?php
function str_replace_once($search, $replace, $subject) {
$newArr = str_split($subject);
$subject = '';
foreach ($newArr as $nchar)
{
$i = array_keys($search, $nchar);
if ($i[0] !== null) $subject .= $replace[$i[0]];
else $subject .= $nchar;
}
return $subject;
}
?>
----
Server IP: 208.69.120.35
Probable Submitter: 69.145.86.254
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/84946
Del: integrated -- https://master.php.net/note/delete/84946/integrated
Del: useless -- https://master.php.net/note/delete/84946/useless
Del: bad code -- https://master.php.net/note/delete/84946/bad+code
Del: spam -- https://master.php.net/note/delete/84946/spam
Del: non-english -- https://master.php.net/note/delete/84946/non-english
Del: in docs -- https://master.php.net/note/delete/84946/in+docs
Del: other reasons-- https://master.php.net/note/delete/84946
Reject -- https://master.php.net/note/reject/84946
Search -- https://master.php.net/manage/user-notes.php