Help needed with php and regex

Discussion in 'PHP' started by parallelsruler, Dec 21, 2010.

  1. #1
    Hi,

    I am using the below code for finding the domain name from a url. I have another code for extracting all child links from a webpage. I was checking the child urls through the below code to see if they are absolute or relative urls. If it is returning a domain, i will conclude that it is absolute and vice versa.

    Problem is occurring when the relative url is something like 'example.html'. The below code is returning domain as 'example.html' itself. Can somebody help me to rewrite the regex to correct this. Thanks!

    Please find the code below.

    preg_match("/^(http:\/\/)?([^\/]+)/i", "$url", $matches);
    $host = $matches[2];
    // get last two segments of host name
    preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
    //echo "domain name is: {$matches[0]}\n";
    $domain = $matches[0];
     
    parallelsruler, Dec 21, 2010 IP