Regex to strip HTML comments

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • redwhiteandblue
    Bollocks
    • Jun 2007
    • 2793

    #1

    Regex to strip HTML comments

    Trying to strip HTML comments from a web page, someone else must have done this.

    I'm using
    Code:
    $page = preg_replace('/<!--(.|\s)*?-->/', '', $page
    which works most of the time but sometimes causes an error. Is there a more reliable one?
    Interserver unmanaged AMD Ryzen servers from $73.00
  • miXXXture
    Confirmed User
    • Jul 2009
    • 217

    #2
    http://php.net/manual/en/function.strip-tags.php
    I use Namesilo for domains and Mojohost, DigitalOcean & Vultr for hosting

    Comment

    • redwhiteandblue
      Bollocks
      • Jun 2007
      • 2793

      #3
      Nope, I don't want to strip all HTML tags - ONLY the HTML commented code (<!-- and --> and everything in between pairs of these).
      Interserver unmanaged AMD Ryzen servers from $73.00

      Comment

      • fris
        Too lazy to set a custom title
        • Aug 2002
        • 55679

        #4
        try <!--(.*?)-->

        instead
        Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.

        Comment

        • redwhiteandblue
          Bollocks
          • Jun 2007
          • 2793

          #5
          Originally posted by fris
          try <!--(.*?)-->

          instead
          Tht's probably what I started with but surely will just catch everything between the first opening comment tag and the last closing one?
          Interserver unmanaged AMD Ryzen servers from $73.00

          Comment

          • cyco_cc
            Confirmed User
            • Oct 2008
            • 344

            #6
            Originally posted by redwhiteandblue
            Tht's probably what I started with but surely will just catch everything between the first opening comment tag and the last closing one?
            No, the '?' part makes it non-greedy so it will match the first closing.

            Edit: When is your regex breaking? The /<!--(.|\s)*?-->/ one?
            Last edited by cyco_cc; 11-24-2009, 08:05 AM.

            Comment

            • Tom_PM
              Porn Meister
              • Feb 2005
              • 16443

              #7
              I was looking all through my old scripts for that to help you, but I think you've got it now.
              43-922-863 Shut up and play your guitar.

              Comment

              • redwhiteandblue
                Bollocks
                • Jun 2007
                • 2793

                #8
                Originally posted by cyco_cc
                Edit: When is your regex breaking? The /<!--(.|\s)*?-->/ one?
                Don't know, on most pages it happily strips out comments but on some it just returns null and I haven't figured out what the difference is with those pages yet.

                EDIT just spotted this comment in the PHP user manual:

                "As a pertinent note, there's an issue with this function where parsing any string longer than 94326 characters long will silently return null. So be careful where you use it at. "

                Could be it.
                Last edited by redwhiteandblue; 11-24-2009, 09:08 AM.
                Interserver unmanaged AMD Ryzen servers from $73.00

                Comment

                Working...