3

I'm trying to get the last part of this url string: national/italy/serie-a/20152016/s11663/

my goal is get: s11663 this is my code:

$path = parse_url("national/italy/serie-a/20152016/s11663/", PHP_URL_PATH);
$pathFragments = explode('/', $path);
$end = end($pathFragments);
var_dump($end);

but I get:

string(0) ""

what I did wrong?

8
  • You can sanitise (remove spaces, slashes, etc from start and end) urls before exploding them. Commented Mar 12, 2016 at 14:52
  • 1
    Please, take a look at the edit of my answer. I added a code to work with urls ending with and without /
    – FirstOne
    Commented Mar 12, 2016 at 15:09
  • @FirstOne I see your second solution but I got this error: Warning: substr() expects parameter 3 to be long, string given in the first, instead, working well. Thanks. Commented Mar 12, 2016 at 15:14
  • @SevengamesXoom, that's odd. It works for me https://3v4l.org/BWjkd... I'll see what I can find
    – FirstOne
    Commented Mar 12, 2016 at 15:16
  • @SevengamesXoom, please, check it forcing the parameter 3 like this (substr($path, -1, 1)) and let me know...
    – FirstOne
    Commented Mar 12, 2016 at 15:21

3 Answers 3

5

I your $path the string contains a / at the end so, when you explode that, the last / also gets counted and the last fragment contains nothing because there is nothing after the last /, So just remove the /.

$path = parse_url("national/italy/serie-a/20152016/s11663", PHP_URL_PATH);
$pathFragments = explode('/', $path);
$end = end($pathFragments);
var_dump($end);

Or if you cannot remove that last /, then just substr the string:

$url = "national/italy/serie-a/20152016/s11663/";
$url = substr($url, 0, strlen($url) - 1);
$path = parse_url($url, PHP_URL_PATH);
$pathFragments = explode('/', $path);
$end = end($pathFragments);
var_dump($end);

Hope it helps you!

Update

In the last example it just substr's the last char get cut, no matter what char, so, what you can do is just as suggested by @firstone's comment, use rather than $url = substr($url, 0, strlen($url) - 1);, is:

$url = rtrim($url, "/");

OR, This:

if($url[strlen($url) - 1] == "/") {
    $url = substr($url, 0, strlen($url) - 1);
} else {
    // Let the url be as it is.....
}
4
  • Note that the second code doesn't work: E_WARNING : type 2 -- substr() expects parameter 3 to be long, string given
    – FirstOne
    Commented Mar 12, 2016 at 15:36
  • Thanks @FirstOne for notifying that! Changed the answer!
    – Ikari
    Commented Mar 12, 2016 at 15:40
  • @Saitama If I pass this: /national/italy/serie-a/c13 will return c1 instead of c13. Commented Mar 12, 2016 at 15:42
  • @Saitama, Just in case the url does not end with / (as in the previous comment ^), you could use $url = rtrim($url, '/'); instead of substr. That way it won't the last char if it's not /...
    – FirstOne
    Commented Mar 12, 2016 at 15:43
3

Because you have a trailing / in the end, you have to get the second last:

$path = parse_url("national/italy/serie-a/20152016/s11663/", PHP_URL_PATH);
$pathFragments = explode('/', $path);
array_pop($pathFragments); // remove last (empty value)
$end = end($pathFragments);
var_dump($end);

Take a look at the output of $pathFragments on your question:

Array
(
    [0] => national
    [1] => italy
    [2] => serie-a
    [3] => 20152016
    [4] => s11663
    [5] => 
)

This code will work for strings ending with and without /:

$path = parse_url("national/italy/serie-a/20152016/s11663", PHP_URL_PATH);
$pathFragmenst = (substr($path, -1) == '/') ? 
    $pathFragments = explode('/', $path, -1) :
    $pathFragments = explode('/', $path);

$end = end($pathFragments);
var_dump($end);

Output for ../s11663/: https://3v4l.org/BWjkd

string(6) "s11663"

Output for ../s11663: https://3v4l.org/H7fP9

string(6) "s11663"


Notes:

1 - This code:

$pathFragmenst = (substr($path, -1) == '/') ? 
    $pathFragments = explode('/', $path, -1) :
    $pathFragments = explode('/', $path);

Is short for this:

if(substr($path, -1) == '/'){
    $pathFragments = explode('/', $path, -1);
}else{
    $pathFragments = explode('/', $path);
}

2 - I don't know if that string is the result of the url_parse, or you are really trying to parse it, but

var_dump(parse_url("national/italy/serie-a/20152016/s11663", PHP_URL_PATH));

will output

string(38) "national/italy/serie-a/20152016/s11663"

, so there is not need to parse_url it - unless you are trying to parse something like:

http://example.com/national/italy/serie-a/20152016/s11663/
^^^^^^^^^^^^^^^^^^^

I left it as is in case you are going to use the complete url...

2

When you do explode('/', $path); the last element of the result array is empty because the last item will be the part after the last "/".

Either use one of the proposed solutions or switch to using preg_match():

$end = '';
$path = parse_url("national/italy/serie-a/20152016/s11663/", PHP_URL_PATH);

if(preg_match('#/([^/]+)/$#', $path, $matches)) {
  $end = $matches[1];
}

var_dump($end);
1
  • It's worth saying that since the op's added this comment, this answer will output string(0) "" in case it's missing the last / too
    – FirstOne
    Commented Mar 12, 2016 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.