Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    103
    Thanks
    13
    Thanked 0 Times in 0 Posts

    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..

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    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

  • #3
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •