Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Expression to remove HTML Comments 1

Status
Not open for further replies.

JimFL

Programmer
Jun 17, 2005
131
0
0
GB
Hi,

Does anybody know how to use the Preg_replace expression to take out HTML comment tags?

I would like to remove all tags like the one below and all the content within it.

<!-- comments within the tag -->

Can anybody help?

 
Can I just add. The reason I am trying to do this is because I am using an HTMLAREA object that is passing some rather nasty code comments through to my posted parameter that I need to remove before it goes into my database. Therefore I should be able to remove it from the posted string?

 
preg_replace("/<!-- [.*] -->/U","",$TheString);

Known is handfull, Unknown is worldfull
 
Thanks alot vbkris. I'll give that a go..
 
I dont know why the expression is not working for me?? But If I output the str to HTML it still apears to be a comment. Have you any ideas why it is not working?
 
can i have the code???

Known is handfull, Unknown is worldfull
 
oops, here is the new one:
/<!--(.*)-->/

change the [] to ()...

Known is handfull, Unknown is worldfull
 
Thanks again.. I should have picked up on that one. One day I get the hang of Preg_replace expressions,
 
I was able to add a comment by adding the code
<!--(comment)-->

In my example, it is a static comment.
 
In case someone comes in here and wants to strip tags away from theire html code, they might want to read up on strip_tags().

If they only want to strip one tag though, the samples above might be a easier way to go, as then they dont have to define all the tags which are allowed.

Olav Alexander Mjelde
Admin & Webmaster
 
Another note:

The expression will not only strip out HTML comments but also Apache SSI directives (which are syntactically like HTML comments but have a # after the opening:
<!--#
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top