1

我尝试解析 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);
4

3 回答 3

4

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);

我不确定你为什么需要这样做,但这应该可以。

演示!

于 2013-09-18T20:06:28.687 回答
2

parse_url。例如:

$url  = 'http://www.phpromania.net/forum/viewtopic.php?f=24&t=7549';
$host = parse_url($url, PHP_URL_HOST);
于 2013-09-18T20:04:18.307 回答
1

首先 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]);
    }
于 2013-09-18T20:04:40.037 回答