2

So, I have dynamically generated pages that follows the following format:

 http://example.com/name/first_name/last_name/John%2C+Smith

What is the php code to output the last part of the url?

so, it becomes like "John, Smith".

Thank you so much.

EDIT:

I realized that the URL is ended with another / and the answers given below does not pick it up. What change should I make?

http://example.com/name/first_name/last_name/John%2C+Smith/

EDIT 2:

So, the link is dynamically generated as the following:

href="http://example.com/name/first_name/last_name/<?php echo $full_name ?>"
Steve Kim
  • 5,293
  • 16
  • 54
  • 99
  • I think this has been answered here: [http://stackoverflow.com/questions/11257232/reverse-htmlspecialchars] – gene Jul 08 '15 at 05:10

3 Answers3

1

Split the url, get the last fragment then URL decode it:

<?
$urlarray=explode("/",$url);
$end=$urlarray[count($urlarray)-1];
$end=urldecode($end);
//go on using $end then
?>
Wahib Mkadmi
  • 627
  • 2
  • 9
  • 26
  • You could run `array_reverse` as well, simplify a step, `$urlarray=array_reverse(explode("/",$url)); echo urldecode($urlarray[0]);`. – chris85 Jul 08 '15 at 05:14
  • Just noticed that the url is ended with another `/` ("/John%2C+Smith/"). What change should I make so that it works? (I updated the question) – Steve Kim Jul 08 '15 at 05:38
  • @steveKim `$url = trim($url, '/');` before `explode` – Hkan Jul 08 '15 at 05:38
1

You could do this with a regex.

echo preg_replace_callback('~.*/(.*)~', 
     function($matches) { 
          return urldecode($matches[1]);
     },
     'http://example.com/name/first_name/last_name/John%2C+Smith');

Regex demo: https://regex101.com/r/bE3bO5/1

Output:

John, Smith

Update:

echo preg_replace_callback('~.*/(.+)~', 
function($matches) { 
     return rtrim(urldecode($matches[1]), '/');
},
'http://example.com/name/first_name/last_name/John%2C+Smith/');
chris85
  • 23,846
  • 7
  • 34
  • 51
  • I realized that there is a `/` at the end and I was wondering what change I should make it order for the code to work. Thank you. – Steve Kim Jul 08 '15 at 05:41
  • Update written that should account for `/` or no `/` at the end. – chris85 Jul 08 '15 at 05:45
  • I have a question. The url is dynamically generated such that last part is always different, for example `http://example.com/name/first_name/last_name/Mike%2C+Tyson/`. In this case, would the answer above work? (I edited my question, see `EDIT 2` please) – Steve Kim Jul 08 '15 at 05:51
  • Yes this would work, also with or without the trailing slash. Give it a try. – chris85 Jul 08 '15 at 12:24
0

You can use parse_url with second parameter PHP_URL_PATH

$url = urldecode("http://example.com/name/first_name/last_name/John%2C+Smith");
$arr = array_filter(explode('/',parse_url($url, PHP_URL_PATH)));
print_r(end($arr));

Edited:

As per requirement for dynamic url you can use

$url = urldecode("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
  • explode returns an array according to http://php.net/manual/en/function.explode.php. You really should not call the array - $str. – gene Jul 08 '15 at 05:25
  • That's a simple variable that I've used over so for better convenience I'll update it to $arr @gene – Narendrasingh Sisodia Jul 08 '15 at 05:29
  • I realized that there is a `/` at the end and I was wondering what change I should make it order for the code to work. Thank you. – Steve Kim Jul 08 '15 at 05:41
  • Check my updated answer I've placed `array_filter` function – Narendrasingh Sisodia Jul 08 '15 at 05:45
  • I have a question. The url is dynamically generated such that last part is always different, for example `http://example.com/name/first_name/last_name/Mike%2C+Tyson/`. In this case, would the answer above work? (I edited my question, see `EDIT 2` please) – Steve Kim Jul 08 '15 at 05:52
  • Hi. so, the answer itself works good. However, the output is stuck at "John, Smith" even when the page is at "Mike, Tyson". How can I change the `$url` variable so that it picks up the last part of the url? (for example, `/John%2C+Smith/` or `/Mike%2C+Tyson/`? – Steve Kim Jul 08 '15 at 06:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82662/discussion-between-uchiha-and-steve-kim). – Narendrasingh Sisodia Jul 08 '15 at 06:07
  • 1
    This answer worked like a charm. I am not sure why there is a downvote on it. – Steve Kim Jul 08 '15 at 21:12