God damn Arrays and echo!

Discussion in 'PHP' started by damien, Jul 31, 2006.

  1. #1
    Hi,

    I'm on the brink of smashing my head against a large rock unless someone can get this script to work. If you manage it, I will be eternally greatful.

    Here's the low-down: The script has URLs in an array, named "$linkarray[1]", therefore each URL is "$linkarray[1][$i]". I use preg_match to take the URLs and return with ONLY the domain and extension. EG: "www.example.com/whatever" into "example.com". Here's the code:

    
    function parseDomainName($host)
    
    {
    
            static $tlds = array('.com', '.net', '.org');
    
     
    
            foreach ($tlds as $tld)
    
           {
    
                    if (preg_match('/^(.*)' . preg_quote($tld) . '$/i', $host, $matches))
    
                    {
    
                            $name_parts = explode('.', $matches[1]);
    
                            return array_pop($name_parts) . $tld;
    
                    }
    
            }
    
            return false;
    
    }
    
     
    
    $urls = $linkarray[1];
    
     
    
    for ($i = 0; $i < count($urls); ++$i)
    
    {
    
           $url_parts = parse_url($linkarray[1][$i]);
    
           $domain = parseDomainName($url_parts['host']);
    
            //echo $urls[0] . "<br />\n";
    		//echo $urls[$i]."<br>";
    		//echo $url[$i];
    		
    
    }
    
    PHP:
    Testing this, it returns nothing but the contents of "$linkarray[1]" It seems to me as if the "$url_parts" and "$domain" part isn't being applied. If it was, it would return the contents of "$linkarray[1]" with each single URL converted into the domain and extension. Could someone please tell me what I should be echoing to get the result? I've tried the three above that are commented out, but only one gives a result, just the untouched contents of the "$linkarray[1]".

    PLEASE help.

    Thank you very much in advance.
     
    damien, Jul 31, 2006 IP
  2. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If all you want to do is pull the domain from the URL, this should do the trick:

    
    #################################################
    # extract the domain portion from a given URL
    function PullDomain( $url)
    {
    if(!isset( $url[3]) ) { return ""; }
    
    $url = preg_replace( '!(ftp|http|https):\/\/!', '', $url);
    
    list($url, $junk) = explode( "/", $url, 2);
    
    return $url;
    }
    
    Code (markup):
     
    clancey, Jul 31, 2006 IP
  3. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #3
    foreach($linkarray[1] as $link) {
    //use clancey's function
    echo PullDomain($link);
    }
    
    PHP:
     
    T0PS3O, Jul 31, 2006 IP
  4. rjb

    rjb Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hey Damien,

    Thought I would just throw this out there...

    
    // Returns array of vals. Ex: array("www", "example", "com"); 
    $host = explode(".", $_SERVER['HTTP_HOST'], 2);
    
    // or to pass in a supplied host
    function parseDomainName($host) {
        $pHost = explode(".", $host, 2);
        return $pHost;
    }
    
    // In case you want this as well. 
    // Returns array of parsed request uri. Ex: array("path" => "/blog")
    $uri = parse_url($_SERVER['REQUEST_URI']);
    
    PHP:
    You can then print out the array to see what parts you need...

    
    print_r ($host);
    print_r (parseDomainName('www.example.com'));
    print_r ($uri);
    
    PHP:
    I hope this helps you out.
     
    rjb, Jul 31, 2006 IP
  5. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #5
    Hey,

    (This is damien by the way, I had to change to another account)

    Thank you SO MUCH guys. It works the closest to what I need. There are only 2 little problems. When I use your code rjb, It shows:

    Array ( [0] => "http://www [1] => parkers.co.uk/" )
    Code (markup):
    For example, how would I stop the first array, array0 from being used? It's created with this line:
    print_r (parseDomainName($linkarray[1][$i]));
    PHP:
    How would I just have [1] of the parsedomainname shown?

    Also, the parsing shows URLs with / after the extension, if you could, would you show how I can "cut off" everything after the / of the URL?

    thank you eternally :)
     
    BRUm, Aug 7, 2006 IP