Php get string after last slash
I want to get the characters after the last / in an url like Show How do I do with php? asked Sep 1, 2009 at 10:40
JohanJohan 18.5k30 gold badges69 silver badges88 bronze badges 1 Very simply:
strrpos gets the position of the last occurrence of the slash; substr returns everything after that position. As mentioned by redanimalwar if there is no slash this doesn't work correctly since
answered Sep 1, 2009 at 10:43
DisgruntledGoatDisgruntledGoat 67.4k65 gold badges204 silver badges285 bronze badges 5
Pikamander2 6,2933 gold badges42 silver badges63 bronze badges answered Sep 1, 2009 at 13:30
GZippGZipp 5,2481 gold badge21 silver badges16 bronze badges 3 You could explode based on "/", and return the last entry:
That's based on blowing the string apart, something that isn't necessary if you know the pattern of the string itself will not soon be changing. You could, alternatively, use a regular expression to locate that value at the end of the string:
answered Sep 1, 2009 at 10:41
SampsonSampson 261k74 gold badges530 silver badges559 bronze badges 5
You can use
SilentGhost 293k64 gold badges301 silver badges291 bronze badges answered Sep 1, 2009 at 11:58
GabrielGabriel 6751 gold badge9 silver badges17 bronze badges 1
answered Sep 1, 2009 at 10:55
ghostdog74ghostdog74 313k55 gold badges252 silver badges339 bronze badges 2
answered Sep 1, 2009 at 10:46
nikc.orgnikc.org 16k6 gold badges47 silver badges83 bronze badges 1 Two one liners - I suspect the first one is faster but second one is prettier and unlike
answered Jan 10, 2017 at 14:35
Eaten by a GrueEaten by a Grue 19.9k10 gold badges79 silver badges99 bronze badges 1 answered Aug 28, 2021 at 1:11
bradbrad 1,19914 silver badges31 bronze badges Here's a beautiful dynamic function I wrote to remove last part of url or path.
answered May 27, 2015 at 12:34
Mahmoud ZaltMahmoud Zalt 28.9k7 gold badges81 silver badges81 bronze badges How to get value after last slash?To get the value of a string after the last slash, call the substring() method, passing it the index, after the last index of a / character as a parameter. The substring method returns a new string, containing the specified part of the original string. Copied!
How can I get the last segment of a string in PHP?strrchr returns the portion of the string after and including the given char, not a numeric index. So you would want to do $last_section = substr(strrchr($string, '. '), 1); to get everything after the char.
How to get last word from url in php?Linked. 150. ... . Get last parameter of url in php.. Get the value of a URL after the last slash.. Get last word in URL and delete what is not necessary.. How to display different data on one php page without using the GET method.. Have app.yaml convert directory path to php script parameter.. Get last parameter from the URL.. How to get string after last slash c#?5 Answers. Since C# 8.0 you can also use the range operator. C# Console. WriteLine(path[pos..]); For reference, see: docs.microsoft.com/en-us/dotnet/csharp/language-reference/… ... . Good thing to notice how this works when there is no slash in the string. It returns the whole string, which is usually correct.. |