Pregunta

mi pregunta relacionada aquí en Así que he llegado con el siguiente fragmento de 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, en especial los bloques duplicados if (strlen($result) > $length) un poco hacker de código. He pensado en dejar caer parse_url() por completo, pero quiero pasar por alto el esquema , usuario , pasar y puerto .

Me pregunto si ustedes pueden llegar a una solución más elegante / organizada que tiene el mismo efecto.


Me acabo de dar cuenta, hay un error - si $depth != 2 el siguiente bloque se ve afectada:

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

Creo que la mejor solución es utilizar un bucle, voy a tratar de solucionar este problema lo antes posible. S


Resuelto que, mediante su sustitución con este nuevo fragmento:

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

Solución 2

Esta es la versión más despejado que se me ocurrió:

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

Otros consejos

Para empezar usted podría tener:

if (is_array($url))

Repetir para todas sus operaciones === true. ¿Por qué estás comparando tipos?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top