best way to delete char# 4-7 from a string?
| From: | Justin French | Date: | Fri, 19 Jul 2002 05:38:32 +0000 |
| Subject: | best way to delete char# 4-7 from a string? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-108440@lists.php.net to get a copy of this message | ||
Hi,
Let's say I have the following string:
$str = 'abcdefghijklmnop';
...and I want to strip out char #'s 4 through to 7 (d,e,f,g)
$strip_start = 4;
$strip_end = 7;
($str would now be 'abchijklmnop';)
THEN I want to plug a new string $filler at position 4
$filler = '4567';
($str would now be 'abc4567hijklmnop')
This is what I think would work (untested), looking for pointers,
optimisation, or a better approach:
<?
$str = 'abcdefghijklmnop';
$strip_start = 4;
$strip_end = 7;
$filler = '4567';
$pre = substr($str,0,$strip_start-1); // abc
$post = substr($str,$strip_end+1,strlen($str)); // hijklmnop
$str = $pre.$filler.$post; //abc4567hijklmnop
?>
Thanks,
Justin French