質問

から 私の関連する質問 ここで私たちは以下の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

こちらではのエ版ん:

$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;
    }
}

他のヒント

まずき:

if (is_array($url))

繰り返しです。 === true ます。なぜ比較すか?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top