note 73305 added to function.str-replace
| From: | rlee0001 at sbcglobal dot net | Date: | Fri, 16 Feb 2007 20:30:57 +0000 |
| Subject: | note 73305 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123497@lists.php.net to get a copy of this message | ||
This is a more rigid alternative to spectrereturns at creaturestoke dot com's replace_different
function:
<?php
function str_replace_many ($search, $replacements, $subject) {
$index = strlen($subject);
$replacements = array_reverse($replacements);
if (count($replacements) != substr_count($subject, $search)) {
return FALSE;
}
foreach ($replacements as $replacement) {
$index = strrpos(substr($subject, 0, $index), $search);
$prefix = substr($subject, 0, $index);
$suffix = substr($subject, $index + 1);
$subject = $prefix . $replacement . $suffix;
}
return $subject;
}
?>
This will return false if there are a different number of $replacements versus number of occurrences
of $search in $subject. Additionally, $search much be exactly one character (if a string is
provided, only the first character in the string will be used). Examples:
<?php
echo str_replace_many('?',array('Jane','banana'),'? is
eating a ?.');
?>
prints: "Jane is eating a banana."
----
Server IP: 64.71.164.2
Probable Submitter: 63.125.44.226
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/73305
Del: integrated -- https://master.php.net/note/delete/73305/integrated
Del: useless -- https://master.php.net/note/delete/73305/useless
Del: bad code -- https://master.php.net/note/delete/73305/bad+code
Del: spam -- https://master.php.net/note/delete/73305/spam
Del: non-english -- https://master.php.net/note/delete/73305/non-english
Del: in docs -- https://master.php.net/note/delete/73305/in+docs
Del: other reasons-- https://master.php.net/note/delete/73305
Reject -- https://master.php.net/note/reject/73305
Search -- https://master.php.net/manage/user-notes.php