how to match this type of construction?
-
05-07-2019 - |
Question
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!!!
Solution
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.
OTHER TIPS
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);