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
Post a Comment