TeNDoLLA Posted August 12, 2009 Share Posted August 12, 2009 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'; Quote Link to comment https://forums.phpfreaks.com/topic/169997-solved-regular-expression-help-needed/ Share on other sites More sharing options...
nrg_alpha Posted August 12, 2009 Share Posted August 12, 2009 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 .*? Quote Link to comment https://forums.phpfreaks.com/topic/169997-solved-regular-expression-help-needed/#findComment-896833 Share on other sites More sharing options...
TeNDoLLA Posted August 13, 2009 Author Share Posted August 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/169997-solved-regular-expression-help-needed/#findComment-896848 Share on other sites More sharing options...
nrg_alpha Posted August 13, 2009 Share Posted August 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/169997-solved-regular-expression-help-needed/#findComment-896866 Share on other sites More sharing options...
TeNDoLLA Posted August 13, 2009 Author Share Posted August 13, 2009 Thanks for your replies and help! I got the problem solved.. was kind of more like stupid user error I had the matching stuff in wrong order compared to the actual html code. Quote Link to comment https://forums.phpfreaks.com/topic/169997-solved-regular-expression-help-needed/#findComment-896872 Share on other sites More sharing options...
nrg_alpha Posted August 13, 2009 Share Posted August 13, 2009 Glad to hear... Please don't forget to flag this thread as solved. Quote Link to comment https://forums.phpfreaks.com/topic/169997-solved-regular-expression-help-needed/#findComment-896876 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.