Jump to content

[SOLVED] Regular expression help needed


TeNDoLLA

Recommended Posts

I have some predefined HTML where I want to match <tr></tr> patterns and stuff between them and replace it with newline depending on the value of the $tag. Anyone can show me the proper pattern for it? Here is some example html and the php code for the pattern that I have tried.

 

<tr>
<td>[:some_tag:]</td>
<td> some text </td>
</tr>
<tr>
<td> [:another_tag:] </td>
<td> another text </td>
</tr>

 

<?php
$pregSearch[] = '/(\r?\n)<tr>\r?\n<td>.*\[:'. $tag .':\].*<\/td>\r?\n<td>.*<\/td>\r?\n<\/tr>\r?\n/';
$pregReplace[] = '$1'; 

Link to comment
https://forums.phpfreaks.com/topic/169997-solved-regular-expression-help-needed/
Share on other sites

One possible solution:

 

Example:

$html = <<<EOD
<tr>
<td>[:some_tag:]</td>
<td> some text </td>
</tr>
<tr>
<td> [:another_tag:] </td>
<td> another text </td>
</tr>
EOD;

$tagName = 'some_tag';
$html = preg_replace('#<tr>\R*<td>\[:'. $tagName .':\]</td>.*?</tr>#is', "\n", $html);
echo $html;

 

This of course makes some rigid assumptions (going by the sample you have listed). If there are tr tags with attributes, simply change <tr> to <tr[^>]*> in the pattern.. same goes for td if need be. This also assumes there is only terminating whitespace characters between <tr> and <td>.. if not, simply change \R* to .*?

No go with this either. And don't really know why. There is newlines after tr and some td tags. And I think the 's' modifier ignores these and removes just everything (should remove) matching this pattern but there will be problem with using the 's' modifier because I don't want to remove all these patterns. Only the ones that has NULL as value for $tagName.

There is newlines after tr and some td tags.

 

Would changing \R* to [\r\n]* help?

 

And I think the 's' modifier ignores these and removes just everything (should remove) matching this pattern but there will be problem with using the 's' modifier because I don't want to remove all these patterns. Only the ones that has NULL as value for $tagName.

 

I think you misunderstand the s modifier. This only applies to the dot in .*? for example. Without that modifier, the dot will stop matching once it hits a newline (as by default, this is the one thing it doesn't match). And this will break the pattern before it can completely finish what needs to be matched.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.