droxlzcgnr639823 2013-02-02 17:19
浏览 34
已采纳

解析php中子域的字符串

How can i find if a string has subdomain existing if there is no scheme / host present.

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

I am trying to parse the url for images, and I am using parse_url for most cases.

But given the url strings can some in different flavors, eg:

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

etc, I am trying to address the different cases one by one. Right now, I am finding it hard to detect if a string has subdomain present or not.

so for a string such as $url="sub.main.com/images/sample.jpg";` i would like to extract the subdomain, and for a string such as images/sample.jpg, i would like to find out that there is no subdomain

  • 写回答

4条回答 默认 最新

  • duanjiao8007 2013-02-02 19:51
    关注

    Interesting problem. I've fiddled around with this for a while; this method inevitably isn't perfect, but it may start you down the right path.

    My solution begins with the 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 an instance of $signingDomainParts = split('\.', $signingDomain); to $signingDomainParts = preg_split('/\./', $signingDomain); if split is deprecated in your php version.

    Once you've got those saved, try this testing code, I put all of the URLs mentioned in the thread here 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 of the 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 )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭