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 -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -