note 64459 deleted from function.str-ireplace by bjori
| From: | bjori@php.net | Date: | Fri, 14 Apr 2006 17:40:03 +0000 |
| Subject: | note 64459 deleted from function.str-ireplace by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-108101@lists.php.net to get a copy of this message | ||
Note Submitter: Kerm Martian (www.cemetech.net)
Reason: in docs
----
Sorry, found a couple of bugs, and added error handlers.
<?php
function strireplace($needle,$replace,$haystack)
{
if ($needle == '') {echo '<B>Error:</B> empty needle
(haystack='.$haystack.')!<BR />'; return $haystack;}
if ($haystack == '') {echo '<B>Error:</B> empty haystack
(needle='.$needle.')<BR />'; return $haystack;}
$where = 0;
$needle = strtolower($needle);
while ($where < strlen($haystack))
{
$thiswhere = @strpos(strtolower($haystack),$needle,$where);
if ($thiswhere === FALSE)
{
$where = strlen($haystack);
} else {
if ($thiswhere == 0)
{
$haystack = $replace . substr($haystack,strlen($needle),strlen($haystack) - strlen($needle));
} else if ($thiswhere == (strlen($haystack) - strlen($needle))) {
$haystack = substr($haystack,0,strlen($haystack) - strlen($needle)) . $replace;
} else {
$haystack = substr($haystack,0,$thiswhere) . $replace .
substr($haystack,$thiswhere+strlen($needle),strlen($haystack) - $thisswhere - strlen($needle));
}
$where=$thiswhere+strlen($replace);
}
}
return $haystack;
}
?>