-1

I have seen on most online newspaper websites that when i click on a headline link, e.g. two thieves caught red handed, it normally opens a url like this: www.example.co.uk/news/two-thieves-caught-red-handed.

How do I deal with this url in php code, so that I can only pick the last part in the url. e.g. two-thieves-caught-red-handed. After that I want to work with this string.

I know how to deal with GET parameters like "www.example.co.uk/news/headline=two thieves caught red handed". But I do not want to do it that way. Could you show me another way.

cb0
  • 8,415
  • 9
  • 52
  • 80
Blessy Khauhelo
  • 125
  • 1
  • 3
  • 11

2 Answers2

0

You can use the combination of explode and end functions for that

for example:

<?php
$url = "www.example.co.uk/news/two-thieves-caught-red-handed";
$url = explode('/', $url);

$end = end($url);
echo "$end";
?>

The code will result

two-thieves-caught-red-handed
Fil
  • 8,225
  • 14
  • 59
  • 85
0

You have several options in php to get the current url. For a detailed overview look here.

One would be to use $_SERVER[REQUEST_URI] and the use a string manipulation function for extraction of the parts you need. Maybe this thread will help you too.

cb0
  • 8,415
  • 9
  • 52
  • 80