Replace the last comma in a string using Regular Expression

前端 未结 2 850
情歌与酒
情歌与酒 2020-12-06 03:21

I have a string like:
\"item 1, item 2, item 3\".
What I need is to transform it to:
\"item 1, item 2 and item 3\".

I

相关标签:
2条回答
  • 2020-12-06 03:53

    This regex finds last coma: (,)[^,]*$

    0 讨论(0)
  • 2020-12-06 04:03

    Use greediness to achieve this:

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

    This matches everything to the last comma and replaces it through itself w/o the comma.

    0 讨论(0)
提交回复
热议问题