3

如果不存在方案/主机,我如何查找字符串是否存在子域。

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

我正在尝试解析图像的 url,并且我在parse_url大多数情况下都在使用。

但是鉴于 url 字符串可以有不同的风格,例如:

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

等等,我试图一一解决不同的情况。现在,我发现很难检测字符串是否存在子域。

所以对于像 $url="sub.main.com/images/sample.jpg";` 这样的字符串,我想提取子域,对于像 images/sample.jpg 这样的字符串,我想找出没有子域

4

4 回答 4

1

有趣的问题。我已经摆弄了一段时间;这种方法不可避免地并不完美,但它可能会让你走上正确的道路。

我的解决方案从这个存储库中的两个源文件开始:https ://github.com/usrflo/registered-domain-libs/tree/master/PHP

首先,如果您的 php 版本中不推荐使用 split ,您可能需要修改 regDomain.inc.php 以更改 to 的实例$signingDomainParts = split('\.', $signingDomain);$signingDomainParts = preg_split('/\./', $signingDomain);

一旦你保存了这些,试试这个测试代码,我把线程中提到的所有 URL 作为测试用例:

<?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/>";

}

?>

此代码将以下测试用例标识为具有子域:

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 )
于 2013-02-02T19:51:53.923 回答
0

试试这个代码

<?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;
}
?>
于 2013-02-02T18:00:45.020 回答
0
<?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));

?>
于 2013-02-02T19:01:53.183 回答
-1

尝试以下操作:

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

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

这应该输出如下内容:

Array ( [0] => sub.main.com [1] => sub [2] => main.com )
于 2013-02-02T17:35:20.650 回答