Chung Leong wrote:
But then you end up replacing every instance of the substring should it
appears more than once. Better to just glue the two remaining ends
together:
substr($s, 0, 4) . substr($s, 4 + 5);
There's another function that can be used if you want to replace only
one sub string, substr_replace(). See
<http://www.php.net/substr_replace>
Example:
<?
$str = 'john goes running and then goes to the store';
echo 'Original string: ' . $str . '<br>';
echo 'Using str_replace: ' .
str_replace(substr($str,4,5),'',$str).'<br>';
echo 'Using substr_replace: ' . substr_replace($str,'',4,5);
?>
Results:
Original string: john goes running and then goes to the store
Using str_replace: john running and then to the store
Using substr_replace: john running and then goes to the store
Ken