2

I want to get the last word of this URL:

http://localhost:8888/articles/tags/Europa

How could I make this in Laravel ? with this {{URL::current()}} I'm getting full URL. I have tried {{URL::current().split('/').last}} but then I get error: Function split() is deprecated

zlotte
  • 1,247
  • 5
  • 22
  • 35

2 Answers2

7

You can also use segment method of the Request class.

$var = Request::segment(3);

This will capture the 3rd segment of the URI according to the API docs: http://laravel.com/api/5.1/Illuminate/Http/Request.html#method_segment

josh088
  • 121
  • 1
  • 7
1

Try and use preg_split() instead

Example:

$var = 'http://localhost:8888/articles/tags/Europa';
$var = preg_split("/\//", 'http://localhost:8888/articles/tags/Europa');
array_pop($var ); //removes last

Update

I can't delete this because it is the accepted answer but please see josh088's answer

Community
  • 1
  • 1
JayIsTooCommon
  • 1,713
  • 2
  • 19
  • 30
  • I tried it like this. Is this correct ? `{{URL::current().preg_split('/').last}}` I'm getting this error: `preg_split() expects at least 2 parameters, 1 given` – zlotte Jul 08 '15 at 20:52
  • Yeah! Now it's working! Thank you! – zlotte Jul 08 '15 at 21:08
  • @feknaz There's better ways of going about this, see josh008's answer. Please change that to the accepted answer so I can remove this :) – JayIsTooCommon Apr 06 '17 at 08:26