views:

57

answers:

2

im doing parsing and the kind of text that i want to match and then make it null is as follows :-

<tr class="label-BGC"><td colspan="4">any kind of text here</td></tr>

i want to match every line that contains "<tr class="label-BGC"><td colspan="4">any text</td></tr>"

its evening here and my brain-battery is totally down

what im trying to do is :-

$patterns='<td colspan="4">'.stristr($parsed,'[^a-z0-9_- $]').'</td></tr>';
$replacements=' ';
$parsed = str_replace($patterns, $replacements, $parsed);

$parsed is containing the whole data that im parsing.

my code is not working can anyone help me with some suggestions here!!!

A: 

Try something simple like this:

$parsed = preg_replace('{<tr class="label-BGC"><td colspan="4">.*?</td></tr>}', 
                      $replacements, $parsed);

The . matches any character, the * makes it match 0-many, and the ? stops it being greedy -i.e. it will stop at the first sequence, and not the last possible one.

Paul Dixon
its not working :-(
developer
Since when does str_replace parse regular expressions?
Justin Johnson
str_replace() doesn't support regular expressions, shouldn't you use preg_replace() instead?
mynameiszanders
thats exactly what i was thinking....
developer
and using preg_replace() has solved my prob....:-)
developer
Sorry, typo on my part
Paul Dixon
A: 

Try the following:

preg_replace("`\s*<tr\s+class\s*=\s*"label-BGC"\s*>\s*<td\s+colspan\s*=\s*"4"\s*>.*?</td\s*>\s*</tr\s*>\s*`i", "", $content);

All the \s matching might be a little overkill, but it's very forgiving if this input is coming from a user.

Otherwise, you can probably just get away with

preg_replace("`<tr class="label-BGC"><td colspan="4">.*?</td></tr>`i", "", $content);
Justin Johnson