如何删除最后一个逗号(第二个逗号),我知道如何使用strpos找到它,但不确定如何删除它。
$m_place = "New York, NY, 10011"; // New York, NY 10011发布于 2011-12-18 08:53:23
怎么样
$tmp = explode(",", $m_place)
$res = $tmp[0].", ".$tmp[1]." ".$tmp[2];发布于 2011-12-18 08:53:35
你也可以用正则表达式的方式做到这一点:
$m_place = str_replace("/,(?=[^,]+$)/", "", $m_place);发布于 2011-12-18 08:51:02
经典的方法是去掉逗号用substr分隔的两部分,并将它们连接起来:
$pos = strrpos($input, ',');
$input = substr($input, 0, $pos).substr($input, $pos + 1);如果要删除任意出现的逗号,一种方便的方法是使用preg_replace的求值功能
$occ = 0; // which occurrence to remove? 0 = first
$i = 0; // need this for the line below
$input = preg_replace('/,/e', '$i++ == $num ? "" : ","', $input);。
https://stackoverflow.com/questions/8549067
复制相似问题