PHP で最後のカンマの後の文字列の一部を削除するには?
文字列:"this is a post, number 1, date 23, month 04, year 2012"
予想:"this is a post, number 1, date 23, month 04"
最後のカンマと残りのカンマを置き換えたいとします。つまり、カンマの後に文字列の終わりまで他の文字が続きます。
これは正規表現として定式化でき、そのパターンはpreg_replace
空の文字列で置き換えることができます:
$until = preg_replace('/,[^,]*$/', '', $string);
これは、文字列にコンマがない場合にも機能するマリオの回答の変形です。