这个片段可以进一步优化/组织吗?

时间:2010-01-14 12:34:09

标签: php regex string explode parse-url

my related question来到这里我已经提出了以下PHP代码段:

$url = parse_url($url);

if (is_array($url))
{
    $depth = 2;
    $length = 50;

    if (array_key_exists('host', $url))
    {
        $result = preg_replace('~^www[.]~i', '', $url['host']);

        if (array_key_exists('path', $url))
        {
            $result .= preg_replace('~/+~', '/', $url['path']); // normalize a bit
        }

        if (array_key_exists('query', $url))
        {
            $result .= '?' . $url['query'];
        }

        if (array_key_exists('fragment', $url))
        {
            $result .= '#' . $url['fragment'];
        }

        if (strlen($result) > $length)
        {
            $result = implode('/', array_slice(explode('/', $result, $depth + 2), 0, $depth + 1)) . '/';

            if (strlen($result) > $length)
            {
                $result = implode('/', array_slice(explode('/', $result, $depth + 1), 0, $depth + 0)) . '/';
            }

            $result = substr($result, 0, $length) . '...';
        }
    }

    return $result;
}

似乎有点hackish,特别是重复的if (strlen($result) > $length)代码块。我考虑完全放弃parse_url(),但我想忽略方案用户传递端口< /强>

我想知道你们是否可以提出一个更优雅/更有条理的解决方案,效果相同。


我刚注意到,有一个错误 - 如果$depth != 2以下块受到影响:

if (strlen($result) > $length)
{
    $result = implode('/', array_slice(explode('/', $result, $depth + 2), 0, $depth + 1)) . '/';

    if (strlen($result) > $length)
    {
        $result = implode('/', array_slice(explode('/', $result, $depth + 1), 0, $depth + 0)) . '/';
    }

    $result = substr($result, 0, $length) . '...';
}

我认为最好的解决方案是使用循环,我会尽力解决这个问题。的:S


解决了这个问题,将其替换为这个新代码段:

if (strlen($result) > $length)
{
    for ($i = $depth; $i > 0; $i--)
    {
        $result = implode('/', array_slice(explode('/', $result), 0, $i + 1)) . '/';

        if (strlen($result) <= $length)
        {
            break;
        }
    }

    $result = substr($result, 0, $length) . '...';
}

2 个答案:

答案 0 :(得分:2)

首先,您可以:

if (is_array($url))

重复所有=== true次操作。你为什么要比较类型?

答案 1 :(得分:0)

这是我提出的不那么混乱的版本:

$url = preg_replace('~^www[.]~i', 'http://www.', array_shift($url));
$parts = parse_url($url);

if (is_array($parts))
{
    $depth = 2;
    $length = 50;

    if (array_key_exists('host', $parts))
    {
        $result = preg_replace('~^www[.]~i', '', $parts['host']);

        if (array_key_exists('path', $parts))
        {
            $result .= preg_replace('~/+~', '/', $parts['path']);
        }

        if (array_key_exists('query', $parts))
        {
            $result .= '?' . $parts['query'];
        }

        if (array_key_exists('fragment', $parts))
        {
            $result .= '#' . $parts['fragment'];
        }

        if (strlen($result) > $length)
        {
            while ($depth > 0)
            {
                $result = implode('/', array_slice(explode('/', $result), 0, $depth-- + 1)) . '/';

                if (strlen($result) <= $length)
                {
                    break;
                }
            }

            $result = substr($result, 0, $length) . '...';
        }

        return $result;
    }
}