Syntax line for subdomain in php

How can I find if a subdomain row exists if there is no schema / host.

eg: $url="sub.main.com/images/sample.jpg";

I am trying to parse url for images and I use parse_url

for most cases.

But given the string strings, some in different flavors, for example:

/images/sample.jpg
//main.com/images/sample.jpg
images/sample.jpg

      

and so on, I am trying to sort out the different cases one by one. Right now I am having a hard time determining if a subdomain is present in a row or not.

so for a string like $ url = "sub.main.com/images/sample.jpg"; `I would like to extract the subdomain, and for a line like images / sample.jpg I would like to know that there is no subdomain

+3


source to share


4 answers


An interesting problem. I've been looking for this for a while; this method is inevitably not perfect, but it can get you started on the right track.

My solution starts with two source files in this repository: https://github.com/usrflo/registered-domain-libs/tree/master/PHP

First, you may need to modify regDomain.inc.php to change the instance $signingDomainParts = split('\.', $signingDomain);

to $signingDomainParts = preg_split('/\./', $signingDomain);

if split is deprecated in your php version.

After you have saved these files try this test code, I put all the urls mentioned in this section as test cases:



<?php
require_once("effectiveTLDs.inc.php");
require_once("regDomain.inc.php");
$tests = Array("/images/sample.jpg","//main.com/images/sample.jpg","images/sample.jpg", "sub.main.com/images/sample.jpg", "http://www.example.com/www.google.com/sample.jpg", "amazon.co.uk/images/sample.jpg", "amazon.com/images/sample.jpg", "http://sub2.sub.main.co.uk/images/sample.jpg", "sub2.sub.main.co.uk/images/sample.jpg");
foreach($tests as $test)
{
    echo "Attempting $test.<BR/>";
    $one = parse_url($test);
    if(!array_key_exists("host", $one))
    {
        echo "Converting to: http://$test";
        echo "<BR/>";
        $one = parse_url("http://$test");
    }
    if(!$one){echo "<BR/>";continue;}
    echo "parse_url parts: ";
    print_r($one);
    echo "<BR/>";
    if($one && array_key_exists("host", $one))
    {
        $domain = getRegisteredDomain($one["host"], $tldTree);
        if(sizeof($domain))
        {
            $two = explode(".", $domain);
            echo "domain parts: ";
            print_r($two);
            echo "<BR/>";
            if(sizeof($two))
            {
                $three = array_diff(explode(".", $one["host"]), $two);
                if(sizeof($three))
                {
                    echo "Hark! A subdomain!: ";
                    print_r($three);
                    echo "<BR/>";
                }
            }
        }
    }
    echo "<BR/>";

}

?>

      

This code identifies the following test cases as having subdomains:

Attempting sub.main.com/images/sample.jpg.
Hark! A subdomain!: Array ( [0] => sub ) 

Attempting http://www.example.com/www.google.com/sample.jpg.
Hark! A subdomain!: Array ( [0] => www ) 

Attempting http://sub2.sub.main.co.uk/images/sample.jpg.
Hark! A subdomain!: Array ( [0] => sub2 [1] => sub ) 

Attempting sub2.sub.main.co.uk/images/sample.jpg.
Hark! A subdomain!: Array ( [0] => sub2 [1] => sub )

      

+1


source


Try this code



<?php
$url = 'sub.main.com/images/sample.jpg';    
$arr = explode('/',$url);

$domain = $arr[0];
$string = $arr[1];

$arr2 = explode('.',$domain);

if(count($arr2)>2) {
    $subdomain = $arr2[0];
    echo $subdomain;
}
?>

      

0


source


<?php
$url = 'http://sub.main.com/images/sample.jpg';
$arr = explode('/',$url);

$pieces = parse_url($url);

$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) 
{ 
  $main_domain=$regs['domain'];
}

$host=$pieces['host'];
$path=$pieces['path'];

if($host != $main_domain)
{
    $arr2 = explode('.',$host); 
    $subdomain = $arr2[0];
    echo $subdomain;
}

$string=substr($path,1,strlen($path));

?>

      

0


source


Try the following:

<?php
$url="sub.main.com/images/sample.jpg";

preg_match('@^(?:http://)?([^.]+).?([^/]+)@i',$url, $hits);
print_r($hits);
?>

      

This should output something like:

Array ( [0] => sub.main.com [1] => sub [2] => main.com )

      

-1


source







All Articles