Re: quick question about preg_replace

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, 05 Oct 2006 09:29:46 +0800, Penthexquadium wrote:

> On Wed, 4 Oct 2006 23:19:58 +0200, "Martin Bach Nielsen" <phpmaillist@xxxxxxxxxxxxx> 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.

If you use this code, be sure to use &lt; in stead of < and &gt; in stead
of > or else the spam messages won't be shown.

Otherwise, if you want the entire message changed, it's quicker to do:

if (preg_match('/(<a href=\"http:\/\/[^^]+)/i', $melding)) {
    $melding = 'This message is temporarily cut-off due to spam-suspicion.';
}

Just an idea...

Ivo

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux