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
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 .*?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.