Re: replace string in array
| From: | Jan Grafstr�m | Date: | Sun, 16 Mar 2003 13:59:41 +0000 |
| Subject: | Re: replace string in array | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-139840@lists.php.net to get a copy of this message | ||
Chris,
you can splice an array
http://www.php.net/manual/sv/function.array-splice.php
Jan Grafstrom
"Chris Winters" <channel0_70@yahoo.com> skrev i meddelandet
news:20030314191347.99802.qmail@pb1.pair.com...
> I'm having a bit of trouble...
>
> I have an array that consists of a string like this:
>
> string1\rstring2\rstring3\rstring4
>
> I want to REMOVE STRING3, and keep the rest of the array intact - but
cannot
> figure on how to do it. I tried using functions such as preg_replace,
strstr
> and so forth...
>
> In my code this is how I go thru the array...
>
> //snippet...
>
> //search for carriage return, split it...
> $line = preg_split( "/[\r]+/" ,$line);
> reset($line);
>
> //Line will consists of array...
> while (list ($key, $val) = each ($line))
> {
> echo "<br>$key => $val";
>
> if(strstr($val,$newmail))
> {
> echo "<br>Found! and trying to delete....<br>";
> //What to do form this point, I have the slightest foggy...
> }
> }
>
> It finds it without ANY problems, but I cannot seem to "go" to the
position
> of found string, delete it, then keep the rest of the array.
> I did manage to find this on php.net, but it doesnt seem to work:
> function stri_replace2 ($search,$replace,$text)
> {
> if (!is_array($search))
> {
> $search = array($search);
> $replace = array($replace);
> }
> foreach($search AS $key => $val)
> {
> $search["$key"] = '/'.quotemeta($val).'/i';
> }
> echo "$search $replace $text";
>
> return preg_replace($search,$replace,$text);
> }
>
> Could anyone help me with this?
>
> Thanks in advanced!
>
> Chris
>
>
>