I have a question which relates to PHP. Is there any way to just grab the last string using a delimiter?
For example, to put you in perspective, I want to grab the lastest content from the /
from every string.
JavaScript
x
/folder1/blabla/important
/folder2/blabla/blabla/bla/important2
How could I using PHP cut the whole string and get only the latest value. The output would be something like:
JavaScript
/important
/important2
I have tried regex but I am not very good at it,
MY CODE:
JavaScript
$var = 'goldfe/sfksfksk/admadmadmam/akdmasdkasm/red';
echo $var . '<br>';
$varPretty = explode('/', $var);
echo $varPretty[-1];
Here the $varPretty[-1]
should return red
.
- ANOTHER SOLUTION IS:
JavaScript
$var = 'goldfe/sfksfksk/admadmadmam/akdmasdkasm/red';
echo $var . '<br>';
$varPretty = explode('/', $var);
echo end($varPretty);
Advertisement
Answer
Please try this method will work for you:
JavaScript
$txt = "/folder1/blabla/important";
echo substr($txt, strrpos($txt, '/') + 1);
Output should be like:
important