View Full Version : PHP Filtering out URL Help.

04-08-2011, 01:02 AM
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..

04-08-2011, 05:39 AM
You'd want preg_replace, something like:

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


This is an extremely over-simplified URI catch and replace, requiring the http:// etc to be present.

04-08-2011, 06:37 PM
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:



$val = preg_replace( '/(http|https)[:][/][/][A-Za-z0-9\-\.]+[.][A-Za-z\.]+/', '', $val );