- strireplace() for PHP3/4
- 14 Apr 2006 10:53:03 am
- Last edited by KermMartian on 14 Apr 2006 12:31:04 pm; edited 6 times in total
Edit: http://us3.php.net/manual/en/function.str-ireplace.php
Enjoy.
Code:
Enjoy.
Code:
<?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;
}
?>