Pergunta

A partir de minha pergunta relacionada Aqui em, então eu criei o seguinte snippet 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;
}

Parece meio hackish, especialmente o duplicado if (strlen($result) > $length) blocos de código. Eu pensei em cair parse_url() no total, mas quero ignorar o esquema, do utilizador, passar e porta.

Estou me perguntando se vocês podem encontrar uma solução mais elegante / organizada que tenha o mesmo efeito.


Acabei de notar, há um bug - se $depth != 2 O bloco a seguir é afetado:

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) . '...';
}

Eu acho que a melhor solução é usar um loop, tentarei consertar isso o mais rápido possível. : S


Resolveu -o, substituindo -o por este novo snippet:

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) . '...';
}
Foi útil?

Solução 2

Aqui está a versão menos desordenada que eu criei:

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

Outras dicas

Para iniciantes que você poderia ter:

if (is_array($url))

Repita para todo o seu === true operações. Por que você está comparando tipos?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top