Domanda

Da la mia domanda relativa qui a COSÌ sono venuto con il seguente 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;
}

Sembra un pò hackish, specialmente il duplicato if (strlen($result) > $length) blocchi di codice.Ho considerato cadere parse_url() complessivamente, ma voglio ignorare il schema di, utente, pass e porta.

Mi chiedo se voi ragazzi può venire con un più elegante / organizzato soluzione che ha lo stesso effetto.


Ho appena notato, c'è un bug - se $depth != 2 il seguente blocco è interessato:

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

Penso che la soluzione migliore è usare un ciclo, vado a tentare di risolvere il problema al più presto. :S


Risolto, sostituendo con questo nuovo frammento di codice:

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) . '...';
}
È stato utile?

Soluzione 2

Qui è il meno affollato versione mi è venuta:

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

Altri suggerimenti

Per cominciare si potrebbe avere:

if (is_array($url))

Ripetere l'operazione per tutti i vostri === true le operazioni.Perché si confrontano i tipi?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top