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

  • SEARCH
  • Home
  • 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 7851239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:07:10+00:00 2026-06-02T19:07:10+00:00

Similar to this question: How to get the last path in a URL? …Except

  • 0

Similar to this question: How to get the last path in a URL?

…Except instead of returning only the last path in the URL, I want to return the last four:

<?php 
  $url = 'http://blogs.mydomain.com/blog-name/2012/04/21/title-of-the-blog-post';
  print_r(parse_url($url));
  $url_path = parse_url($url, PHP_URL_PATH);
  $parts = explode('/', $url_path);
  $relative_permalink = end($parts);
echo $relative_permalink;
?>

The code above is from the earlier Stack Overflow item I linked to. It kind of does what I want, except it returns only title-of-blog-post; I want to return everything after /blog-name/, like this:

/20120/04/21/title-of-blog-post

This code also sort of gets me there:

$url_endpoint = http://blogs.mydomain.com/blog-name/2012/04/21/title-of-the-blog-post;
$url_endpoint = parse_url( $url_endpoint );
$url_endpoint = $url_endpoint['path'];

Except that it also returns /blog-name/ and everything after it. I want to exclude /blog-name/

Any ideas for how I can manipulate either of these snippets?

  • 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-06-02T19:07:12+00:00Added an answer on June 2, 2026 at 7:07 pm

    Try array_slice:

    $relative_permalink = array_slice($parts, -4);
    

    –Update–

    $url = 'http://blogs.mydomain.com/blog-name/2012/04/21/blog-title/';
    $url_path = parse_url($url, PHP_URL_PATH);
    
    // We are exploding on /, so there may be some empty array elements
    // passing them through array_filter will remove them
    $parts = array_filter(explode('/', $url_path), 'strlen');
    
    // Grab the last 4 elements of array
    $relative_permalink = array_slice($parts, -4);
    
    // Put humpty back together again
    echo implode('/', $relative_permalink);
    

    Your question had a hard req of the last 4, which this will accomplish, however Blair McMillan’s answer is more flexible as it will allow you to take anything after ‘blog-name’, so keep that in mind.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a similar question as How to get the common name for a
Similar to this question only the other way of flow. Insert Picture into SQL
Similar to this question , except the console program being wrapped up in the
Different from this question , but similar in that I don't get an error
Okay this is similar to my last question but what I ended up doing
I've been reading similar questions to this issue and have been able to get
Similar to this question: link However I have already mastered that. My problem is
Similar to this question I have a custom subclass of UITableViewCell that has a
This question is similar to this one How do I add options to a
This question is similar to this other one , with the difference that the

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.