Jump to content


Photo

str_replace empty value question


  • Please log in to reply
1 reply to this topic

#1 avenged

avenged
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 20 April 2006 - 06:47 AM

hello, was just wondering how I would be able to use str_replace to skip an index in an array if something is found in str_replace. I have a str_replace that checks an array for values in an array.

str_replace($array1, "", $array2);

it searches array2 with values from array1, and i was wondering, if it finds a match, instead of it replacing it with a space, how do i replace it with nothing, as in the array returned wont even have an index with anything in it with that?. say i am searching an array with values:

array('1', '2', '3');

and i want to search it from the values contained in this array:
array('1');
so, obviously, it would return a match with this array:
array('', '2', '3');

How would I replace the match with nothing, so it would return an array like this:
array('2', '3');

??

Thanks!

#2 slashemail

slashemail
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 20 April 2006 - 08:47 AM

One clear cut solution would be.

Get the size of the array, Loop through the aray and replace each instance in the array.


for ($i=0;$i<sizeof($array1);$i++)
{
  if ($array1[$i] == "What ever")       #strcmp could also be used
    {
       $array1[$i] = "";    #Use any value 
     }
}

You can use your own logic to read the array & replace the occurences found within.

Hope this will puts some light.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users