note 21735 modified in function.str-replace by aidan
| From: | aidan@php.net | Date: | Fri, 25 Jun 2004 06:07:55 +0000 |
| Subject: | note 21735 modified in function.str-replace by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71645@lists.php.net to get a copy of this message | ||
If you want to replace only the first occurence of a string you can use this function:
<?php
function str_replace_once($needle, $replace, $haystack) {
// Looks for the first occurence of $needle in $haystack
// and replaces it with $replace.
$pos = strpos($haystack, $needle);
if ($pos === false) {
// Nothing found
return $haystack;
}
return substr_replace($haystack, $replace, $pos, strlen($needle));
}
?>
--was--
If you want to replace only the first occurence of a string you can use this function:
function str_replace_once($needle, $replace, $haystack) {
// Looks for the first occurence of $needle in $haystack
// and replaces it with $replace.
$pos = strpos($haystack, $needle);
if ($pos === false) {
// Nothing found
return $haystack;
}
return substr_replace($haystack, $replace, $pos, strlen($needle));
}
http://php.net/manual/en/function.str-replace.php