GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Regex to strip HTML comments (https://gfy.com/showthread.php?t=940068)

redwhiteandblue 11-24-2009 06:19 AM

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?

miXXXture 11-24-2009 06:25 AM

http://php.net/manual/en/function.strip-tags.php

redwhiteandblue 11-24-2009 06:40 AM

Quote:

Originally Posted by miXXXture (Post 16580084)

Nope, I don't want to strip all HTML tags - ONLY the HTML commented code (<!-- and --> and everything in between pairs of these).

fris 11-24-2009 07:06 AM

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

instead

redwhiteandblue 11-24-2009 08:51 AM

Quote:

Originally Posted by fris (Post 16580212)
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?

cyco_cc 11-24-2009 10:03 AM

Quote:

Originally Posted by redwhiteandblue (Post 16580464)
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?

Tom_PM 11-24-2009 10:07 AM

I was looking all through my old scripts for that to help you, but I think you've got it now.

redwhiteandblue 11-24-2009 11:04 AM

Quote:

Originally Posted by cyco_cc (Post 16580883)
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.


All times are GMT -7. The time now is 12:12 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc