Goldeneye Posted November 25, 2009 Share Posted November 25, 2009 The following function attempts to match "<!--$tag--> [html-MARKUP]<!--$tag-->" in $this->page.$this->page is a string that contains HTML-Markup from an HTML-Template file.$tag would be something like "repeat.content" But right now it isn't matching despite there being a <!--repeat.content-->*<!--/repeat.content--> in $this->page, it still echo's "Template-Looping error!" This leads me to believe that my regular-expression is wrong. So, my question -- is there anything wrong with my regular-expression? <?php function repeat($tag, $replacements){ if(preg_match_all('/\<\!\-\-'.$tag.'\-\-\>(.*?)\<\!\-\-\/'.$tag.'\-\-\>/i', preg_quote($this->page, '/'), $match)){ $str = str_replace('<!--'.array_keys($replacements).'-->', array_values($replacements), $match[0]); echo $str; } else echo '<p>Template-Looping error!</p>'; } ?> Link to comment https://forums.phpfreaks.com/topic/182862-matching-html-comments-using-pcre/ Share on other sites More sharing options...
cags Posted November 25, 2009 Share Posted November 25, 2009 A pattern something like... '#(<!--'.$test.'-->)(.*?)\1#is' ...should work. Don't use preg_quote on the input, use it on $tag if needed. Link to comment https://forums.phpfreaks.com/topic/182862-matching-html-comments-using-pcre/#findComment-965335 Share on other sites More sharing options...
Goldeneye Posted November 26, 2009 Author Share Posted November 26, 2009 Of course, the brackets! I always forget that those sort of things should be encased in brackets, as a subpattern. Thank you very, cags. On to my next obstacle, now. Link to comment https://forums.phpfreaks.com/topic/182862-matching-html-comments-using-pcre/#findComment-965756 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.