7 Replies - 2643 Views - Last Post: 17 March 2011 - 09:52 AM Rate Topic: -----

#1 dsoppelsa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-October 10

PHP Regular Expression HTML Comment Matching

Posted 16 March 2011 - 03:37 PM

Hi everyone,

I am trying to use php regex to strip html comments from a string. I am able to match and replace a single line html comment such as '<!--html comment-->'. The problem is that if there are newline characters in the comment such as:
<!-- comment start
A line of text
Another line of text
end of comment
-->

I cannot find a regular expression that matches this. I have tried '/<!--(.*[\n\r])*-->/'

Any thoughts?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Regular Expression HTML Comment Matching

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: PHP Regular Expression HTML Comment Matching

Posted 16 March 2011 - 03:56 PM

You might give /<!--[\w\s]+?-->/ a go and see how that does.

This post has been edited by Valek: 16 March 2011 - 03:56 PM

Was This Post Helpful? 1
  • +
  • -

#3 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,303
  • Joined: 22-October 08

Re: PHP Regular Expression HTML Comment Matching

Posted 16 March 2011 - 05:46 PM

If you would like to lern more about regx i wrote a blog some monts back:
MyBlog

Hope it helps :)
Was This Post Helpful? -1
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: PHP Regular Expression HTML Comment Matching

Posted 16 March 2011 - 05:57 PM

I would recommend avoiding the use of POSIX RegEx in PHP, as it is deprecated.
Was This Post Helpful? 1
  • +
  • -

#5 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,303
  • Joined: 22-October 08

Re: PHP Regular Expression HTML Comment Matching

Posted 16 March 2011 - 06:19 PM

I wasn't quite fimiliar with it so i used POSIX :) but i showed how to convert :)
Was This Post Helpful? 0
  • +
  • -

#6 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: PHP Regular Expression HTML Comment Matching

Posted 16 March 2011 - 07:39 PM

Awesome website for help understanding regular expressions:
http://www.regular-expressions.info/
An awesome tool for helping you test them out
http://regex.powertoy.org/

Also, you may want to look at the pattern modifiers. Specifically, the 's' modifier should work if you do something like:

/<!--.*-->/s

This post has been edited by grimpirate: 16 March 2011 - 07:44 PM

Was This Post Helpful? 3
  • +
  • -

#7 EarthShaker  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 186
  • Joined: 16-March 11

Re: PHP Regular Expression HTML Comment Matching

Posted 17 March 2011 - 09:05 AM

preg_replace('/<!--(.*?)-->/', '', $blah)

should do.. Just remember that using regular expression to modify html code is not a good practice.

Peace ;)

This post has been edited by EarthShaker: 17 March 2011 - 09:08 AM

Was This Post Helpful? 1
  • +
  • -

#8 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: PHP Regular Expression HTML Comment Matching

Posted 17 March 2011 - 09:52 AM

Nice link with powertoy grimpirate, I wasn't familiar with that one although I believe your first link got fubared(you can still copy/paste the text of course). I also reccomend RegExr as another tool to test your regular expressions.

Like most things in life you get better with regular expressions with practice. I have a fairly good understanding of them and their use but don't use them allot at my current job so am rusty. I've heard Mastering Regular Expressions is a good book to read on the subject(there are very little written specifically about regular expressions) and have been meaning to pick it up when I have time to go through it.

Good luck :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1