Skip to content
Advertisement

PHP Parsing Getting the latest value from a string using a delimiter

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.

/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:

/important
/important2

I have tried regex but I am not very good at it,

MY CODE:

$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:
$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:

$txt = "/folder1/blabla/important";
echo substr($txt, strrpos($txt, '/') + 1);

Output should be like:

important

enter image description here

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement