Jump to content

Find Open HTML Comments?


Perad

Recommended Posts

Just a minor note to the OP, the pattern you posted probably doesn't match what you think it does. I doubt you'll run into any situation where it'll make much difference, but it's better to have it correct. All preg_ patterns must have delimiters generally speaking this is the same non-alphanumeric character before and after the pattern, but there are exceptions to this and bracket style character 'pairs' can be used. For this reason your pattern is not actually matching the < at the start and the > at the end. It is instead using them as the delimiters. To see what I mean (if you can't from my description) run this string though your current pattern...

 

This is an example !--this isn't a comment-- it's part of my text.

 

You should consider adding delimiters to the pattern.

 

'#<!--(.*?)-->#'

How do I find a '<!--' that is missing a '-->'?

 

if (!preg_match('#(?><!--.*?(?=<!--|-->|$))(?!-->)#si', $html))
{
echo 'Your HTML tag is valid because it contains both opening and closing comment tags';
}
else
{
echo 'Your HTML tag is invalid because it doesn't contain a closing tag --> for the opening <!-- comment tag';

}

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.