Link to home
Start Free TrialLog in
Avatar of Victor Kimura
Victor KimuraFlag for Canada

asked on

strpos() but starting from end then delete the last word + the comma at end PHP

Hi,

I'm trying to find the position of the last "," (comma) in this list:

roth ira contribution,ira contribution limits,traditional ira,roth ira,excess contributions,simple ira

then delete the last comma till the end of the list so the word list will look like:
roth ira contribution,ira contribution limits,traditional ira,roth ira,excess contributions

I was looking at strpos() but that won't work as this list will vary in length.

Thank you,
vkimura
Avatar of Chimeraza
Chimeraza
Flag of South Africa image

This should work for you...

mb_strrpos will give you the last position of the ','
substr will truncate the string from position 0 to the position of the ','

<?php
$string = 'rot,h ira c,ontribu,tion';
echo substr($haystack, 0, mb_strrpos ($string , ','));
?>

Kind regards
Nick
ASKER CERTIFIED SOLUTION
Avatar of Chimeraza
Chimeraza
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Victor Kimura

ASKER

Thanks, Chimeraza. I thought it would be something simple like that.