Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-08-2011, 01:02 AM   PM User | #1
dk4210
New Coder

 
Join Date: Nov 2002
Posts: 99
Thanks: 13
Thanked 0 Times in 0 Posts
dk4210 is an unknown quantity at this point
PHP Filtering out URL Help.

Hello Guys,

I want to be able to filter out any url in any of form $_POST vars? Would I do it with a foreach loop and the preg replace function?

I would consider any web address in my form spam. I would like to filter it out..

I'm already using
Strip tags, htmlentities, strip_tags, stripslashes & mysql_real_escape_string but they don't seem to filter out URLs..

Thanks for your help in advanced..
dk4210 is offline   Reply With Quote
Old 04-08-2011, 05:39 AM   PM User | #2
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
You'd want preg_replace, something like:

PHP Code:
$val preg_replace('/[a-z]{1,6}:\/\/[^\s]*/i'' '$val); 
Untested.

This is an extremely over-simplified URI catch and replace, requiring the http:// etc to be present.
__________________
lamped.co.uk :: Design, Development & Hosting
marcgray.co.uk :: Technical blog
Lamped is offline   Reply With Quote
Old 04-08-2011, 06:37 PM   PM User | #3
ShaneC
Codeasaurus Rex


 
Join Date: Jun 2008
Location: Redmond, WA
Posts: 659
Thanks: 31
Thanked 100 Times in 94 Posts
ShaneC is on a distinguished road
Lamped is correct, a preg_replace is probably your best bet.

Here is an alternative REGEX, finding specifically HTTP or HTTPS, and then ending with the domain:

Code:
^(http|https)[:][/][/][A-Za-z0-9\-\.]+[.][A-Za-z\.]+$
Implying:

PHP Code:
$val preg_replace'/(http|https)[:][/][/][A-Za-z0-9\-\.]+[.][A-Za-z\.]+/'''$val ); 
__________________
Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
My posts, comments, code, and suggestions reflect only my personal views.
Web Portfolio and Code Snippets: http://shanechism.com
ShaneC is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:35 AM.


Advertisement
Log in to turn off these ads.