php - Replace the last comma in a string using Regular Expression -



php - Replace the last comma in a string using Regular Expression -

i have string like: "item 1, item 2, item 3". need transform to: "item 1, item 2 , item 3".

in fact, replace lastly comma " and". can help me this?

use greediness accomplish this:

$text = preg_replace('/(.*),/','$1 and',$text)

this matches lastly comma , replaces through w/o comma.

php regex preg-replace

Comments

Popular posts from this blog

java - How to pass parameters between Request-Scoped Controllers? -

string - what is the difference between "some" == "some\0" and strcmp("some","some\0") in c++? -

actionscript 3 - Flex: moving a point with rotation doesnt change the point XY? -