Re: quick question about preg_replace

From: Date: Thu, 05 Oct 2006 01:29:46 +0000
Subject: Re: quick question about preg_replace
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-242629@lists.php.net to get a copy of this message
On Wed, 4 Oct 2006 23:19:58 +0200, "Martin Bach Nielsen" <phpmaillist@martinbn.info> wrote: > Hi all. > > > > I have written a guestbook (I know, there's a ton out there), which until > recently did not get any spam, so now I'm trying to remove the spam-ad by > using preg_replace: > > > > $melding = preg_replace('/(<a > href=\"http:\/\/[^^]+)/i', "This message is > temporarily cut-off due to spam-suspicion.", $melding); > > > > However, this only makes the problem half-solved, since I also want any text > before the link to be replaced with a message as stated above. The above > line removes anything after '<a href=//'. > > Is there any way to make the above line to include anything before the <a > href=// ? > > > > I have tried different options, and yes, I have read the php manual on > preg_replace, but I might not have properly identified how to get the text > in front modified. > > > > Thankful for any hints, tips, links, anything that helps :-) > > > > Regards, > > Martin Bach Nielsen > If you only want to replace the line including url(s) by a message, a simple regular expression is enough. <?php $melding = "This is a spam.\n" . "This is a <a href=\"http://www.example.com/\">example</a> line.\n" . "Another line.\n" . "<a href=\"http://www.e.com/ads/show?n=123\">123</a> (456)\n" . "End Line.\n"; $melding = preg_replace("/.*?<a href=\"http:\/\/.+/i", "<This line is temporarily cut-off due to spam-suspicion.>", $melding); echo $melding; ?> The script above will output: This is a spam. <This line is temporarily cut-off due to spam-suspicion.> Another line. <This line is temporarily cut-off due to spam-suspicion.> End Line. -- Sorry for my poor English.

« previous php.general (#242629) next »