bundyxc Posted December 4, 2009 Share Posted December 4, 2009 I'm looking throughout an entire HTML document for something in this format: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title><!--{template:title}--></title> <link rel="stylesheet" type="text/css" href="my.css"> </head> <body> <h1><!--{template:header}--></h1> <p><!--{template:paragraph}--></p> </body> </html> I'd like to look through the file, and have matches of: title, header, paragraph. What's the code I would use to do this? I was using this: \<!--{template:(.*)}--> But unfortunately, that simply matches the entire HTML comments. I'm assuming that this has to do with lookarounds. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/183925-simple-regex-is-this-correct/ Share on other sites More sharing options...
rajivgonsalves Posted December 4, 2009 Share Posted December 4, 2009 try this <?php $str = <<< EOS <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title><!--{template:title}--></title> <link rel="stylesheet" type="text/css" href="my.css"> </head> <body> <h1><!--{template:header}--></h1> <p><!--{template:paragraph}--></p> </body> EOS; preg_match_all('#\{template:(.*?)\}#', $str, $matches); print_r($matches); ?> Quote Link to comment https://forums.phpfreaks.com/topic/183925-simple-regex-is-this-correct/#findComment-970940 Share on other sites More sharing options...
bundyxc Posted December 4, 2009 Author Share Posted December 4, 2009 Output: Array ( [0] => Array ( [0] => {template:title} [1] => {template:header} [2] => {template:paragraph} ) [1] => Array ( [0] => title [1] => header [2] => paragraph ) ) Quote Link to comment https://forums.phpfreaks.com/topic/183925-simple-regex-is-this-correct/#findComment-970948 Share on other sites More sharing options...
cags Posted December 4, 2009 Share Posted December 4, 2009 What do you mean "it matches the entire comment?" The part in the brackets is the first capture group and can be found in the array $matches[1]. It can be done with lookbehind and lookahead assertions, but do you really need to? The pattern would turn into something along the lines of... but you should probably avoid using the fullstop if only certain characters are valid (such as letters only). Also some of these characters may well need escaping, I'm not on a computer I can test it with atm. #(?<=<!--{template:)(.*)(?=}-->)#isU Quote Link to comment https://forums.phpfreaks.com/topic/183925-simple-regex-is-this-correct/#findComment-970950 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.