Forum Moderators: coopster

Message Too Old, No Replies

preg_replace() assistance

         

erikcw

8:14 pm on Jul 2, 2005 (gmt 0)

10+ Year Member



Hi,

I'm tring to do the following:

Perform a preg_replace on a list of items seperated by \n ($_POST['html_textarea']) with another list of items ($_POST['textarea_replace']).


$html_textarea = "item a
item b
product d
something e
something f";


$textarea_replace = "a
b";

The pattern I've used before is:
$find = "blah";
/[^\n]*$find.*)\n/Ui


$output = preg_replace("$textarea_replace", $replacement, $html_textarea);

End Result:


$output="product d
something e
something f";

So how do I get this to work? The pattern above works great if there is just one item to match in $textarea_replace, but since I need to work with a list, it is messing everything up. I tried using an array of patterns, but can't seem to get it working.

What should I do to make this thing work?

Thanks so much for your help! This is driving me nuts!

coopster

11:08 pm on Jul 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I tried absorbing this a few times but am still a bit perplexed. What exactly are you trying to find and replace here?

erikcw

7:34 pm on Jul 14, 2005 (gmt 0)

10+ Year Member



ok, all lists are seperated by \n

I want to take all the "items" from list-a, and match them against list-b. It's kind of like a multiple find and replace. The script will search list-b for lines that are contained in list-a.

Does that clarify?

Thanks!

coopster

8:58 pm on Jul 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Unless the two lists will always have the same number of array elements (lines), you are probably going to have to construct a double loop. The outer loop will read the pattern and the inner loop will match/replace. Then read the next pattern and match/replace, etc.