Re: remove from a string
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.php archive

Re: remove from a string

From: Ken Robinson <kenrbnsn@rbnsn.com>
Date: Fri Aug 26 2005 - 07:04:00 CEST

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
Received on Mon Oct 24 02:09:34 2005