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.

 

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

Link to comment
Share on other sites

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';

}

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.