解析复杂的URL(Parsing complex URLs)

2019-10-18 15:45发布

我尝试解析URL字符串列表,每工作两小时后,我没有达到任何结果,URL字符串列表如下所示:

$url_list = array(
    'http://google.com',
    'http://localhost:8080/test/project/',
    'http://mail.yahoo.com',
    'http://www.bing.com',
    'http://www.phpromania.net/forum/viewtopic.php?f=24&t=7549',
    'https://prodgame10.alliances.commandandconquer.com/12/index.aspx',
    'https://prodgame10.alliances.commandandconquer.ro/12/index.aspx',
);

输出应该是:

Array
(
    [0] => .google.com
    [1] => .localhost
    [2] => .yahoo.com
    [3] => .bing.com
    [4] => .phpromania.net
    [5] => .commandandconquer.com
)

是什么促使我在错误区域的第一件事是在URL中超过2个点。 任何算法的例子?


这是我尝试:

    $url_list = array(
        'http://google.com',
        'http://localhost:8080/test/project/',
        'http://mail.yahoo.com',
        'http://www.bing.com',
        'http://www.phpromania.net/forum/viewtopic.php?f=24&t=27549',
        'https://prodgame10.alliances.commandandconquer.com/12/index.aspx',
    );

    function size($list)
    {
        $i=0;
        while($list[++$i]!=NULL);
        return $i;
    }

    function url_Host($list)
    {
        $listSize = size($list)-1;
        do
        {
            $strSize = size($list[$listSize]);
            $points = 0;
            $dpoints = 0;
            $tmpString = '';
            do
            {
                $currentChar = $list[$listSize][$strSize];
                if(ord('.')==ord($currentChar))
                {
                    $tmpString .= '.';
                    $points++;
                }
                else if(ord(':')==ord($currentChar))
                {
                    $tmpString .= ':';
                    $dpoints++;
                }
            }while($list[$listSize][--$strSize]!=NULL);
            print $tmpString;
            $strSize = size($list[$listSize]);
            $tmpString = '';
            do
            {
                $slice = false;
                $currentChar = $list[$listSize][$strSize];
                if($dpoints > 2)
                {
                    if(ord('\\')==ord($curentChar)) $slice = true;
                    $tmpString .= '';
                }
            }while($list[$listSize][--$strSize]!=NULL);
            print $tmpString."<br />";
        }while($list[--$listSize]);
    }

    url_Host($url_list);

Answer 1:

您可以使用内置函数parse_url()如下:

function getDomain($url) 
{
    $domain = implode('.', array_slice(explode('.', parse_url($url, PHP_URL_HOST)), -2));
    return $domain;
}

测试用例:

foreach ($url_list as $url) {
    $result[] = getDomain($url);
}

输出:

Array
(
    [0] => google.com
    [1] => localhost
    [2] => yahoo.com
    [3] => bing.com
    [4] => phpromania.net
    [5] => commandandconquer.com
    [6] => commandandconquer.ro
)

至于点,你可以手动将其前面加上字符串,就像这样:

$result[] = "." . getDomain($url);

我不知道为什么你需要做到这一点,但这应该工作。

演示!



Answer 2:

看看parse_url 。 例如:

$url  = 'http://www.phpromania.net/forum/viewtopic.php?f=24&t=7549';
$host = parse_url($url, PHP_URL_HOST);


Answer 3:

首先localhost的结果是没有意义的,但试试这个:

$result =array();

    foreach($url_list as $u){
       $arr = explode('//',$u);
       $arr2 = explode('.', $arr[1]);
       if($arr2[0] == 'www')
           array_push($result, $arr2[1]);
       else
           array_push($result, $arr2[0]);
    }


文章来源: Parsing complex URLs
标签: php arrays url