Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    JAG
    JAG is offline
    New Coder
    Join Date
    Feb 2011
    Posts
    63
    Thanks
    24
    Thanked 0 Times in 0 Posts

    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]-->

  • #2
    Regular Coder
    Join Date
    Jul 2012
    Location
    London
    Posts
    473
    Thanks
    4
    Thanked 86 Times in 86 Posts
    try this:

    Code:
        $('*').contents().each(function() {
            if(this.nodeType == 8) {
                $(this).remove()
            }
        });

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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 ); 
    Last edited by kbluhm; 01-12-2013 at 06:33 PM.

  • Users who have thanked kbluhm for this post:

    JAG (01-15-2013)

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,632
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    JAG (01-15-2013)

  • #5
    JAG
    JAG is offline
    New Coder
    Join Date
    Feb 2011
    Posts
    63
    Thanks
    24
    Thanked 0 Times in 0 Posts
    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.

  • #6
    New Coder
    Join Date
    Dec 2012
    Location
    England
    Posts
    20
    Thanks
    0
    Thanked 4 Times in 4 Posts
    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.

  • Users who have thanked linek98 for this post:

    JAG (01-15-2013)

  • #7
    JAG
    JAG is offline
    New Coder
    Join Date
    Feb 2011
    Posts
    63
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Thanks go out to kbluhm, felgall and linek98!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •