Check if link is valid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thew
    New Member
    • Aug 2010
    • 69

    Check if link is valid

    I want a snippet that check if the website link is valid. For example:

    www.example

    returns FALSE

    www.example.com

    returns TRUE

    www.example.co. uk

    returns TRUE

    www.lol.lol.lol .lol

    returns FALSE

    lol.example.com

    returns TRUE




    But how can i check that?
    Last edited by Atli; Sep 27 '10, 03:36 PM. Reason: Please try not to use real URLs as example values.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    There are two methods, in general, to do things like that. String manipulation and Regular Expressions. - I would be very surprised if you couldn't find a handful of examples for both via Google.

    However, in the case of emails and URLs, you can use the filter_var function instead. (See the example.)

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      Welcome back Thew,

      With a regular expression with PHP's preg_match(). There are some you can find on google. you might need to tweak it for your use.

      Regex Buddy is a tool I love to do regexps with. It also has some libraries of built-in expressions you can use.

      To start you off, here's what yours might look like:

      Code:
      ^[a-z]+\.[a-z]+\.([a-z]{3}|[a-z]{2}\.[a-z]{2})$
      The above matches with your examples above but will fail for domains like

      customer.suppor t.company.com


      Cheers,

      Dan

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        www.lol.lol.lol .lol

        returns FALSE
        why should that not be an URL? from the syntax point-of-view, this validates as URL, although the TLD (Top Level Domain) is currently invalid

        Comment

        Working...