Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 561347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:26:26+00:00 2026-05-13T12:26:26+00:00

From my related question here at SO I’ve come up with the following PHP

  • 0

From my related question here at SO I’ve come up with the following PHP snippet:

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

Seems kinda hackish, specially the duplicate if (strlen($result) > $length) blocks of code. I’ve considered dropping parse_url() altogether, but I want to ignore the scheme, user, pass and port.

I’m wondering if you guys can come up with a more elegant / organized solution that has the same effect.


I just noticed, there is a bug – if $depth != 2 the following block is affected:

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

I think the best solution is to use a loop, I’ll try to fix this ASAP. :S


Solved it, by replacing it with this new 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) . '...';
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:26:26+00:00Added an answer on May 13, 2026 at 12:26 pm

    Here is the less cluttered version I came up with:

    $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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Swiftly following on from my last TripleDES-related question , here's my TripleDES wrapper code,
Question related to PHP memory-handling from someone not yet very experienced in PHP: If
Following on from my question here , I'm trying to create a generic value
This is a CSS related question, I got one good answer from my previous
My question is related to the design-time support of WPF. From MSDN I read,
This is related to my previous question , regarding pulling objects from a dmp
In general, I have the following scenario: Fetch product and its related data from
I've asked a related question here: Show unmatched html tags in Notepad++ Having only
This question here is related to Haskell Input Return Tuple I wonder how we
I'm kinda new here and I've got a programming/coding related question you guys maybe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.