URL Validation: JavaScript Regex to PHP Regex

Discussion in 'PHP' started by OpenCode, Dec 20, 2010.

  1. #1
    Hello,

    I got a JavaScript URL Validation Regex, since I'm not used-to with Regex I need help converting it into PHP regex.

    Here is a JavaScript Regex




    I need this particular regex in PHP


    Thanks in advance
     
    Last edited: Dec 20, 2010
    OpenCode, Dec 20, 2010 IP
  2. underground-stockholm

    underground-stockholm Guest

    Messages:
    53
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if (preg_match('|^https?://(www\d{0,4}\.)?([-_A-Za-z0-9]+\.)?[-_.A-Za-z0-9]+(\.[A-Za-z]{2,6})([-0-9a-zA-Z_%&?~/.=]*)$|', $url))
            echo "yes\n";
    else
            echo "no\n";
    PHP:
     
    underground-stockholm, Dec 20, 2010 IP
  3. OpenCode

    OpenCode Greenhorn

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Underground-stockholm,

    Thanks for your contribution but I couldn't find the match with the Regex you've provided to me..
    I tested the following url and I doubt if the javascript regex I've does this for me..

    
    $url="http://www.facebook.com/profile.php?id=66665555#!/home.php?sk=group_66665555&ap=1";
    
    Code (markup):

    I'm waiting for the improved regex

    thanks
     
    OpenCode, Dec 21, 2010 IP
  4. ankit_frenz

    ankit_frenz Active Member

    Messages:
    1,111
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    63
    #4
    there is no best regexp to match urls as modern age design rules changes heavily.You will need to analayse specific situation and see whats best for you.As usually there is a single ? in urls but as your urls as more than one so this regex wont work..there can be also troubles in matching relative and absolute urls..So first find what you need and code accordingly.
    Hope that helps
     
    ankit_frenz, Dec 21, 2010 IP
  5. Matty Asia

    Matty Asia Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This wouldn't work for international domain names, or any addresses that have non english characters. Just a note.
     
    Matty Asia, Dec 23, 2010 IP