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

Posted on 2009-04-16
Medium Priority
Last Modified: 2012-05-06

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,
Question by:Victor Kimura
  • 2

Expert Comment

ID: 24155411
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 ','

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

Kind regards

Accepted Solution

Chimeraza earned 2000 total points
ID: 24155421
Sorry, should be....

$string = 'rot,h ira c,ontribu,tion';
echo substr($string, 0, mb_strrpos ($string , ','));

Author Closing Comment

by:Victor Kimura
ID: 31570863
Thanks, Chimeraza. I thought it would be something simple like that.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question