I’m trying to find a way to break a string at the second last comma, for example:
piece 1, piece 2, piece 3, piece 4, piece 5, piece 6, piece 7 should be 2 parts:
piece 1, piece 2, piece 3, piece 4, piece 5 and
piece 6, piece 7
and
piece 1, piece 2, piece 3, piece 4, piece 5 should be:
piece 1, piece 2, piece 3 and
piece 4, piece 5
Is there a string manipulation to search the string for a character and identify the position of the second last instance of that character?
I thought about exploding the string by , then gluing the last 2 to make part 2, and gluing however many first ones (varies) to make part 1, but I think that might be overkill. Any string manipulations for this?
Use
strrposto find the position of the last comma.Use
strrposwith the last position as the offset to find the 2nd last comma.Break the string with
substr.