php extract variable from a string using regexp

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

    #1

    php extract variable from a string using regexp

    Hi can someone give me hand with this please?


    What's the best way to extract the extension from the url?

    example:
    $string="http://www.domain.co.u k/anypage.html"
    In this example, I'd be looking for: "co.uk" but it could be "com",
    "net" , or any other extension

    Thanks
    Ciarán

  • Rik

    #2
    Re: php extract variable from a string using regexp

    On Tue, 17 Jul 2007 21:28:00 +0200, Ciaran <cronoklee@hotm ail.comwrote:
    Hi can someone give me hand with this please?
    >
    >
    What's the best way to extract the extension from the url?
    >
    example:
    $string="http://www.domain.co.u k/anypage.html"
    In this example, I'd be looking for: "co.uk" but it could be "com",
    "net" , or any other extension
    Why? And there is no easy way to allow for 'double extentions' like co.uk
    whithout you giving it a list of accepted doubles. There's no 'logical'
    way to allow for this.

    Well, to get the TLD:

    $string ="http://www.domain.co.u k/anypage.html";

    //NON REGEX WAY:
    $urlinfo = parse_url($stri ng);
    $domaincomponen ts = explode('.',$ur linfo['host']);
    $extention = end($domaincomp onents);

    //REGEX
    preg_match('%
    ^ #match at start
    (?:[a-z]+://)? #possible protocol
    [^/]*? #domainstring
    ([^/.]+) #TLD
    (?:/|$) #start of path or end of string
    %six',$string,$ match);
    $extention = $match[1];

    If you want to allow for doubles, you'll have to provide a list of
    acceptable 'doubles', and match it to the end of the hostname.

    --
    Rik Wasmus

    Comment

    • Toby A Inkster

      #3
      Re: php extract variable from a string using regexp

      Rik wrote:
      If you want to allow for doubles, you'll have to provide a list of
      acceptable 'doubles', and match it to the end of the hostname.


      If I get bored this evening I might have a go.

      --
      Toby A Inkster BSc (Hons) ARCS
      [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
      [OS: Linux 2.6.12-12mdksmp, up 27 days, 17:51.]

      PHP Linkifier

      Comment

      • Ciaran

        #4
        Re: php extract variable from a string using regexp

        On Jul 17, 8:59 pm, Rik <luiheidsgoe... @hotmail.comwro te:
        On Tue, 17 Jul 2007 21:28:00 +0200, Ciaran <cronok...@hotm ail.comwrote:
        Hi can someone give me hand with this please?
        >
        What's the best way to extract the extension from the url?
        >
        example:
        $string="http://www.domain.co.u k/anypage.html"
        In this example, I'd be looking for: "co.uk" but it could be "com",
        "net" , or any other extension
        >
        Why? And there is no easy way to allow for 'double extentions' like co.uk
        whithout you giving it a list of accepted doubles. There's no 'logical'
        way to allow for this.
        >
        Well, to get the TLD:
        >
        $string ="http://www.domain.co.u k/anypage.html";
        >
        //NON REGEX WAY:
        $urlinfo = parse_url($stri ng);
        $domaincomponen ts = explode('.',$ur linfo['host']);
        $extention = end($domaincomp onents);
        >
        //REGEX
        preg_match('%
        ^ #match at start
        (?:[a-z]+://)? #possible protocol
        [^/]*? #domainstring
        ([^/.]+) #TLD
        (?:/|$) #start of path or end of string
        %six',$string,$ match);
        $extention = $match[1];
        >
        If you want to allow for doubles, you'll have to provide a list of
        acceptable 'doubles', and match it to the end of the hostname.
        >
        --
        Rik Wasmus
        Hey thanks a lot Rik, This helps a lot!

        Comment

        • Toby A Inkster

          #5
          Re: php extract variable from a string using regexp

          Toby A Inkster wrote:

          If I get bored this evening I might have a go.
          OK -- I've written a PHP class that is capable of extracting whichever
          components you like out of a hostname, including its "effective TLD"
          that is:

          Host ETLD
          --------------------------------------------
          groups.google.c o.uk. co.uk.
          www.ealing.nhs.uk. nhs.uk.
          www.ipart.nsw.gov.au. nsw.gov.au.
          www.google.com. com.
          www.last.fm. fm.
          www.british-library.uk. uk.
          del.icio.us. us.

          The syntax for this is:

          <?php
          include "Domain.class.p hp";
          $x = new Domain('www.eal ing.nhs.uk');
          echo $x->get_reg_domain ()."\n"; // ealing.nhs.uk.
          echo $x->get_etld()."\n "; // nhs.uk.
          echo $x."\n"; // www.ealing.nhs.uk.
          ?>

          Link to download is in my signature below...

          --
          Toby A Inkster BSc (Hons) ARCS
          [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
          [OS: Linux 2.6.12-12mdksmp, up 28 days, 13:08.]

          PHP Domain Class

          Comment

          Working...