Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-12-2013, 12:55 PM   PM User | #1
JAG
New Coder

 
Join Date: Feb 2011
Posts: 51
Thanks: 17
Thanked 0 Times in 0 Posts
JAG is an unknown quantity at this point
Question Removing HTML Comments

Hi guys,

I'm one of those who loves typing a lot of comments in my HTML pages, but would prefer those comments not to be visible if a user does a "view source" on a page. What's the best way--using PHP--to strip comments likes these:

Code:
<!-- Some note here. -->

<!--
       Some
       long
       note
       here.
-->
It should NOT strip conditional comments:

Code:
<!--[if IE 7]>
<link rel="stylesheet" href="/css/ie7.css" type="text/css" media="screen" />
<![endif]-->
JAG is offline   Reply With Quote
Old 01-12-2013, 02:55 PM   PM User | #2
tempz
Regular Coder

 
Join Date: Jul 2012
Location: London
Posts: 436
Thanks: 4
Thanked 80 Times in 80 Posts
tempz is an unknown quantity at this point
try this:

Code:
    $('*').contents().each(function() {
        if(this.nodeType == 8) {
            $(this).remove()
        }
    });
tempz is offline   Reply With Quote
Old 01-12-2013, 06:13 PM   PM User | #3
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,503
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
Quote:
Originally Posted by tempz View Post
try this:

Code:
    $('*').contents().each(function() {
        if(this.nodeType == 8) {
            $(this).remove()
        }
    });
The OP specifically emphasized wanting a PHP solution.

This will strip all comments, but exclude thise that begin with <!--[
PHP Code:
$html preg_replace'/\<\!\-\-[^\[].*\-\-\>/Us'''$html ); 
__________________
ZCE

Last edited by kbluhm; 01-12-2013 at 06:33 PM..
kbluhm is offline   Reply With Quote
Users who have thanked kbluhm for this post:
JAG (01-15-2013)
Old 01-12-2013, 09:03 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,530
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Simplest PHP solution is to use PHP comments instead of HTML comments in the first place.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
JAG (01-15-2013)
Old 01-13-2013, 03:09 AM   PM User | #5
JAG
New Coder

 
Join Date: Feb 2011
Posts: 51
Thanks: 17
Thanked 0 Times in 0 Posts
JAG is an unknown quantity at this point
Quote:
Originally Posted by kbluhm View Post
The OP specifically emphasized wanting a PHP solution.

This will strip all comments, but exclude thise that begin with <!--[
PHP Code:
$html preg_replace'/\<\!\-\-[^\[].*\-\-\>/Us'''$html ); 

This seems to be what I'm looking for, but how do I implement it as a function? PHP n00b here.
JAG is offline   Reply With Quote
Old 01-14-2013, 01:33 AM   PM User | #6
linek98
New Coder

 
Join Date: Dec 2012
Location: England
Posts: 18
Thanks: 0
Thanked 4 Times in 4 Posts
linek98 is an unknown quantity at this point
PHP Code:
function removeHTMLComments($html) {
  return 
preg_replace'/\<\!\-\-[^\[].*\-\-\>/Us'''$html );  
}

echo 
removeHTMLComments($html); 
If you have multiple echos/prints each displaying part of website then it's best that you unify them into simple variable and echo/print it all at once so you wont preg_replace multiple times.
linek98 is offline   Reply With Quote
Users who have thanked linek98 for this post:
JAG (01-15-2013)
Old 01-15-2013, 09:31 AM   PM User | #7
JAG
New Coder

 
Join Date: Feb 2011
Posts: 51
Thanks: 17
Thanked 0 Times in 0 Posts
JAG is an unknown quantity at this point
Thanks go out to kbluhm, felgall and linek98!
JAG is offline   Reply With Quote
Reply

Bookmarks

Tags
comments, html, php, remove, strip

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:05 PM.


Advertisement
Log in to turn off these ads.