1

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?

Community
  • 1
  • 1
laurenmichell
  • 45
  • 1
  • 4

5 Answers5

1
list(, $last_four) = explode('/blog-name/', $url);
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • a) It doesn't necessarily give the last 4 parts, just everything after '/blog-name/' - whatever that is b) It depends on the path the file is in, which it shouldn't – ccKep Apr 27 '12 at 01:15
  • Which is what she's looking for. It's a wordpress blog. It's going to always be in that format. – John Conde Apr 27 '12 at 01:16
  • He's looking for the last 4 parts of a URL, your code might work for his example. It might not for the actual use cases. – ccKep Apr 27 '12 at 01:16
  • Re-read the question. *She* wants *everything* after `/blog-name/`. My code does that even if the format changes. – John Conde Apr 27 '12 at 01:17
  • ´I want to return the last four´ Don't get me wrong, I never said your solution is invalid - there are just more cleaner ways in my opinion (see Mike Purcells array_slice) – ccKep Apr 27 '12 at 01:20
  • Cleaner then a simple one-liner? – John Conde Apr 27 '12 at 01:20
  • Because the length of code directly relates to how clean it is? As I said: It will probably work (what happens if the post title equals the blog-name though? Just a question...) – ccKep Apr 27 '12 at 01:23
  • And the odds of that happening is...? – John Conde Apr 27 '12 at 01:23
  • Even a simple `str_replace("http://blogs.mydomain.com/blog-name", "", $url);` or `substr(...)` would work. There are many different ways to solve this, I'm just rating them. I apologize if this hurts your feelings... – ccKep Apr 27 '12 at 01:32
1

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.

Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
1

If you want everything after the first one, wouldn't it be easier to array_shift() off the first element and return the others?

<?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);
  $static_name = array_shift($parts);
  $relative_permalink = implode('/', $parts);
  echo $relative_permalink;
?>
Blair McMillan
  • 5,299
  • 2
  • 25
  • 45
0

Try:

<?php 
$url = 'http://blogs.mydomain.com/blog-name/2012/04/21/title-of-the-blog-post';
$url_path = parse_url($url, PHP_URL_PATH);
$parts = explode('/', $url_path);
$relative_permalink = implode('/', array_slice($parts, -4));
echo $relative_permalink;
?>
ccKep
  • 5,786
  • 19
  • 31
0

To get the path in a URL, you can use parse_url:

$url = 'http://blogs.mydomain.com/blog-name/2012/04/21/title-of-the-blog-post';
$urlInfo = parse_url($url);
print_r($urlInfo);
$urlPath = $urlInfo['path'];
echo "\n";
PenguinCoder
  • 4,335
  • 1
  • 26
  • 37