php - How to get the sub string from the start until the second last comma? -


from string this:

$a = "viale giulio cesare, 137, roma, rm, italia"; 

i need string until tne penultimate comma:

$b = "viale giulio cesare, 137, roma"; 

how can remove finding penultimate comma?

this should work you:

here first last comma in string strrpos(). out of sub string search last comma, penultimate comma. position of second last comma substr() of entire string.

echo substr($a, 0, strrpos(substr($a, 0, strrpos($a, ",")), ","));    //^^^^^^        ^^^^^^^ ^^^^^^        ^^^^^^^    //|             |       |             |1.returns position of last comma $a    //|             |       |2.get substr() start $a until last comma    //|             |3.returns position of last comma substring    //|4.get substr start $a until position of second last comma 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -