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

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -