1

I learned how to parse an URL and return me a specific part of it.

For now, I'm currently working in a localhost server, which contains a long basename:
localhost/mydocs/project/wordpress/mexico/cancun

If I want to get the word mexico I would have to count 4 until there.

$url   = localhost/mydocs/project/wordpress/mexico/cancun
$parse = parse_url($url); 
$path  = explode('/', $parse[path]);
echo   = $path[4]

Even though it works fine for localhost, when uploading in the server, the basename get shorter and the number 4 can not reach mexico, because the URL becomes:
example.com/mexico/cancun

I'd like to know if there is a global solution for it. I thought about counting backwards, like using -2, so it would start counting from the word "cancun", but I don't know whether is possible or not!

Thank you!

Caio Ferrari
  • 319
  • 1
  • 3
  • 13

1 Answers1

0

use $path[count($path)-2] -2 being the configurable part.

Note this will only work for numeric indices, like for your case.

t3chguy
  • 1,018
  • 7
  • 17