Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

daniel21gt's avatar

How can I get the last digit in the url, laravel 5.5

How about, I have the following line

dd(request()->headers->get('referer'));

which shows me this

"http://proyectodos.test/panel/selectord?valorusuario=3"

I have no idea if I am possible, but I want to extract the last digit, the number 3, can I?

0 likes
9 replies
Cronix's avatar
Cronix
Best Answer
Level 67

There are lots of ways to do that. It's important to know though, whether it will always be the last character in the url. Is it a pattern, in other words.

If you know it will always be the last char in the string, you can just grab it.

$lastChar = substr(request()->headers->get('referer'), -1);

You can also use regex and actually grab digits from the string. That might work, but again, it depends on if there is a pattern to look for. There could be a number in 2 different places in the url or something.

1 like
daniel21gt's avatar

I understand perfectly, thank you very much cronix, as always with the correct answer ..

simon66's avatar

@CRONIX - @daniel21gt keep in mind that this solution will not work on number greater than 9 (ex =valorusuario132). You might want to look at regex or something more viable.

1 like
Cronix's avatar

@simon66 True, which is why I made the 2nd suggestion (which would be solid) and mentioned that it depends on what kind of pattern there is (which was never stated). You'd be able to get the value directly from the query string as a variable since it will make an array of the keys/values and you can just grab the value for valorusario. Even regex would have problems as I pointed out (if url has 2 different places for digits and isn't constant, etc).

2 likes
azurinspire's avatar

I think @cronix meant something like this:

$url = request()->headers->get('referer'); // http://proyectodos.test/panel/selectord?valorusuario=3
parse_str(parse_url($url)['query'], $output);

echo $output['valorusuario']; // 3
1 like
daniel21gt's avatar

Thanks eparcel_fi, as I did it works, but as you do it is much more professional, I have a lot to learn.

1 like

Please or to participate in this conversation.