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

Posted on 2009-04-16
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
    LVL 3

    Expert Comment

    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
    LVL 3

    Accepted Solution

    Sorry, should be....

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

    Author Closing Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now