Validate domain name context

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sam

    Validate domain name context

    Validate domain name context

    Hello , Today I want to make sure from a given text is a domain style
    or not.

    for example : the domain can be

    example.com or example.com.us or example.us so , what is the pattren
    to check the given string is a domain style or not?

    note that the first text must be at least 2 chars and the second text
    may be 2 or 3 chars and the last text must be 2 chars or null.

    Thanks,
    Sam Shannaq

  • bill

    #2
    Re: Validate domain name context

    Sam wrote:
    Validate domain name context
    >
    Hello , Today I want to make sure from a given text is a domain style
    or not.
    >
    for example : the domain can be
    >
    example.com or example.com.us or example.us so , what is the pattren
    to check the given string is a domain style or not?
    >
    note that the first text must be at least 2 chars and the second text
    may be 2 or 3 chars and the last text must be 2 chars or null.
    >
    Thanks,
    Sam Shannaq
    >
    can also be example.info

    bill

    Comment

    • Sam

      #3
      Re: Validate domain name context

      yes sure

      so any one have the pattren to check?

      On Jan 27, 10:17 pm, bill <nob...@spamcop .netwrote:
      Sam wrote:
      Validate domain name context
      >
      Hello , Today I want to make sure from a given text is a domain style
      or not.
      >
      for example : the domain can be
      >
      example.com or example.com.us or example.us so , what is the pattren
      to check the given string is a domain style or not?
      >
      note that the first text must be at least 2 chars and the second text
      may be 2 or 3 chars and the last text must be 2 chars or null.
      >
      Thanks,
      Sam Shannaqcan also be example.info
      >
      bill- Hide quoted text -- Show quoted text -

      Comment

      • Sagari

        #4
        Re: Validate domain name context

        There's also '.museum' TLD, e.g.

        example.museum

        Non-national domain names can only have digits, Latin letters and a
        dash in them; dash can't be first or last character. So you can begin
        with this pattern:

        #^(([a-z0-9][-a-z0-9]*?[a-z0-9])\.)+[a-z]{2,6}$#

        That will match all valid 3-rd or higher level domain names as well

        However, national domain names can have non-ASCII letters.

        Best wishes,

        Konstantin

        On 28 ñÎ×., 02:12, "Sam" <m.shan...@gmai l.comwrote:
        yes sure
        >
        so any one have the pattren to check?
        >
        On Jan 27, 10:17 pm, bill <nob...@spamcop .netwrote:
        >
        Sam wrote:
        Validate domain name context
        >
        Hello , Today I want to make sure from a given text is a domain style
        or not.
        >
        for example : the domain can be
        >
        example.com or example.com.us or example.us so , what is the pattren
        to check the given string is a domain style or not?
        >
        note that the first text must be at least 2 chars and the second text
        may be 2 or 3 chars and the last text must be 2 chars or null.
        >
        Thanks,
        Sam Shannaqcan also be example.info
        >
        bill- Hide quoted text -- Show quoted text -

        Comment

        • Toby Inkster

          #5
          Re: Validate domain name context

          Sam wrote:
          so any one have the pattren to check?
          Please don't top post.

          How sure do you want to be that a domain is real? Do you just want to make
          sure that the domain is lexically sane? If so, you want to check that the
          domain name has 2 or more components, each of which contains at least two
          alphanumeric characters and may contain hyphens, though the hyphen may not
          be the first or last character. Each component should terminate with a dot.
          Code here:

          $domain = 'me.example.net .';

          // Note the domain ends with a dot! This is the correct form for
          // a fully qualified domain name, even though most people tend to
          // leave out the final dot. Including the dot makes the regular
          // expression check much easier, so here we want to make sure that
          // $domain ends with a dot, and if not add it.
          if (!preg_match('/\.$/', $domain)) $domain .= '.';

          // Now check if it meets syntax rules.
          $domain_ok = preg_match('/^([a-z0-9][a-z0-9\-]*[a-z0-9]\.){2,}$/',
          $domain);

          This check will allow for a domain like 'foobar.quux.qu uux.' even though
          it is obviously not a real domain though. To check that it's a real,
          registered domain name, which points to a real IP address, we can use
          gethostbyname() :

          // Firstly, do all the above stuff to make sure it it syntactically
          // correct. No point wasting network resources calling gethostbyname()
          // for a domain that is obviously invalid. Now:

          $domain_ok = $domain_ok && !empty(gethostb yname($domain)) ;

          // Now $domain_ok is TRUE iff $domain is an Internet host. Yay!

          --
          Toby A Inkster BSc (Hons) ARCS
          Contact Me ~ http://tobyinkster.co.uk/contact

          Comment

          • Sagari

            #6
            Re: Validate domain name context

            Three objections:

            1. Not all the registered domain names have valid 'A' record. In that
            case, gethostbyname() won't return anything meaningful for registered
            domain. You'll have to address WHOIS database directly and that isn't
            an easy and quick trick, generally.

            2. Domain names check might be required to register that domain.
            Again, gethostbyname(0 won't return anything useful.

            3. In case the corresponding nameservers are unreachable at the
            moment, gethostbyname() won't return anything valid

            I only wished to say that gethostbyname() doesn't fit all the cases
            and, in general, it makes validation significantly slower.

            Best wishes,

            Konstantin
            This check will allow for a domain like 'foobar.quux.qu uux.' even though
            it is obviously not a real domain though. To check that it's a real,
            registered domain name, which points to a real IP address, we can use
            gethostbyname() :
            >
            // Firstly, do all the above stuff to make sure it it syntactically
            // correct. No point wasting network resources calling gethostbyname()
            // for a domain that is obviously invalid. Now:
            >
            $domain_ok = $domain_ok && !empty(gethostb yname($domain)) ;
            >
            // Now $domain_ok is TRUE iff $domain is an Internet host. Yay!
            >
            --
            Toby A Inkster BSc (Hons) ARCS
            Contact Me ~http://tobyinkster.co.uk/contact

            Comment

            • Toby Inkster

              #7
              Re: Validate domain name context

              Sagari wrote:
              I only wished to say that gethostbyname() doesn't fit all the cases
              and, in general, it makes validation significantly slower.
              Please do not top post.

              I did mention towards the end, that it actually checks if $domain is a
              valid Internet host. Depending on why the OP needs to "validate" the
              domain name this may or may not be important.

              --
              Toby A Inkster BSc (Hons) ARCS
              Contact Me ~ http://tobyinkster.co.uk/contact

              Comment

              • Sam

                #8
                Re: Validate domain name context

                When I do

                $domainName = "google.com ";
                $res = ereg('#^(([a-z0-9][-a-z0-9]*?[a-z0-9])\.)+[a-z]{2,6}$#',
                $domainName)

                res was false :( and I got this warning

                Warning: ereg() [function.ereg]: REG_BADRPT

                Thanks

                On Jan 28, 4:50 am, "Sagari" <konstan...@boy andin.ruwrote:
                There's also '.museum' TLD, e.g.
                >
                example.museum
                >
                Non-national domain names can only have digits, Latin letters and a
                dash in them; dash can't be first or last character. So you can begin
                with this pattern:
                >
                #^(([a-z0-9][-a-z0-9]*?[a-z0-9])\.)+[a-z]{2,6}$#
                >
                That will match all valid 3-rd or higher level domain names as well
                >
                However, national domain names can have non-ASCII letters.
                >
                Best wishes,
                >
                Konstantin
                >
                On 28 ñÎ×., 02:12, "Sam" <m.shan...@gmai l.comwrote:
                >
                >
                >
                yes sure
                >
                so any one have the pattren to check?
                >
                On Jan 27, 10:17 pm, bill <nob...@spamcop .netwrote:
                >
                Sam wrote:
                Validate domain name context
                >
                Hello , Today I want to make sure from a given text is a domain style
                or not.
                >
                for example : the domain can be
                >
                example.com or example.com.us or example.us so , what is the pattren
                to check the given string is a domain style or not?
                >
                note that the first text must be at least 2 chars and the second text
                may be 2 or 3 chars and the last text must be 2 chars or null.
                >
                Thanks,
                Sam Shannaqcan also be example.info
                >
                bill- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -

                Comment

                • Michael Fesser

                  #9
                  Re: Validate domain name context

                  ..oO(Sam)
                  >When I do
                  >
                  >$domainName = "google.com ";
                  >$res = ereg('#^(([a-z0-9][-a-z0-9]*?[a-z0-9])\.)+[a-z]{2,6}$#',
                  >$domainName)
                  >
                  >res was false :( and I got this warning
                  >
                  >Warning: ereg() [function.ereg]: REG_BADRPT
                  Forget ereg*(), use the PCRE functions instead. They are much more
                  flexible and the preferred regex engine in PHP 6.

                  Micha

                  Comment

                  • Toby A Inkster

                    #10
                    Re: Validate domain name context

                    Sam wrote:
                    ereg('#^(([a-z0-9][-a-z0-9]*?[a-z0-9])\.)+[a-z]{2,6}$#',$domai nName)
                    IIRC, the ereg() function doesn't expect any starting or ending characters
                    the the regular expression (the hash marks in the above).

                    Use preg_match() -- it's faster, more flexible and expects the starting and
                    ending characters!

                    --
                    Toby A Inkster BSc (Hons) ARCS
                    Contact Me ~ http://tobyinkster.co.uk/contact
                    Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

                    * = I'm getting there!

                    Comment

                    Working...