Solved

Remove HTML Comment Tags Unless They Contain Certain Words

Posted on 2014-01-03
4
287 Views
Last Modified: 2014-01-03
I have HTML code stored in the variable, $page_entire_code. I am trying to strip out all HTML comment tags except ones that contain the word, "Test1" or "Test2". My current code is:

$page_entire_code =~ s/<!--.+-->//g;

Thanks!
0
Comment
Question by:webstuck5
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39754734
What you have now is quite dangerous. You have a "greedy" pattern. What it will do is remove everything between the very first opening comment tag, to the very last closing comment tag, not the comment tag that closes the first opening tag. This is bad. You need a non-greedy version of your pattern. You simply need one additional character:

$page_entire_code =~ s/<!--.+?-->//g;

Open in new window


The addition question mark makes the pattern not greedy. Now the pattern will match an opening comment tag up until the very next closing comment tag.

Now, this doesn't solve your overall issue. We need to tweak your pattern just a bit more. To look for certain words you will need to employ a lookahead. For your needs, you will need a negative lookahead. Let's try the following pattern:

$page_entire_code =~ s/<!--(?!.*?(Test[12]).*?-->).*?-->//g;

Open in new window

0
 

Author Comment

by:webstuck5
ID: 39754763
I had tested my greedy pattern on HTML code that had a lot of comment tags and it only looked to remove each comment tag and not the HTML in between. If I used the words, "TEST_ON" and "TEST_OFF" would your code still work, if I did:

$page_entire_code =~ s/<!--(?!.*?(TEST_[OFFON]).*?-->).*?-->//g;
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39754785
Ah, I forgot one important detail:  The dot will match, by default, everything that is not a newline character. So if each of your comments is contained on its own line, then you're fine.

For this new requirement, that could work, but I'd prefer an alternation (i.e. an OR condition--the vertical bar) myself:

$page_entire_code =~ s/<!--(?!.*?(TEST_(OFF|ON)).*?-->).*?-->//g;

Open in new window

0
 

Author Closing Comment

by:webstuck5
ID: 39754835
Thanks so much!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to create a tile layout 1 36
Datepicker in PHP 9 38
Form Processing in PHP 11 30
Footer at the bottom of the page not centered 5 27
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now