Php 删除strpo
如何剥离最后一个逗号(第二个逗号),我知道如何使用strpos定位它,但不确定如何删除它Php 删除strpo,php,Php,如何剥离最后一个逗号(第二个逗号),我知道如何使用strpos定位它,但不确定如何删除它 $m_place = "New York, NY, 10011"; // New York, NY 10011 经典的方法是去掉逗号与substr分隔的两部分,并将它们连接起来: $pos = strrpos($input, ','); $input = substr($input, 0, $pos).substr($input, $pos + 1); 如果要删除任意出现的逗号,一种方便的方法是使用pr
$m_place = "New York, NY, 10011"; // New York, NY 10011
经典的方法是去掉逗号与
substr
分隔的两部分,并将它们连接起来:
$pos = strrpos($input, ',');
$input = substr($input, 0, $pos).substr($input, $pos + 1);
如果要删除任意出现的逗号,一种方便的方法是使用preg\u replace
的求值功能:
$occ = 0; // which occurrence to remove? 0 = first
$i = 0; // need this for the line below
$input = preg_replace('/,/e', '$i++ == $num ? "" : ","', $input);
经典的方法是将逗号与
substr
分隔的两部分切掉并连接起来:
$pos = strrpos($input, ',');
$input = substr($input, 0, $pos).substr($input, $pos + 1);
如果要删除任意出现的逗号,一种方便的方法是使用preg\u replace
的求值功能:
$occ = 0; // which occurrence to remove? 0 = first
$i = 0; // need this for the line below
$input = preg_replace('/,/e', '$i++ == $num ? "" : ","', $input);
您只需将该位置的字符设置为
'
您只需将该位置的字符设置为
'
怎么样
$tmp = explode(",", $m_place)
$res = $tmp[0].", ".$tmp[1]." ".$tmp[2];
怎么样
$tmp = explode(",", $m_place)
$res = $tmp[0].", ".$tmp[1]." ".$tmp[2];
您也可以通过正则表达式的方式来实现:
$m_place = str_replace("/,(?=[^,]+$)/", "", $m_place);
您也可以通过正则表达式的方式来实现:
$m_place = str_replace("/,(?=[^,]+$)/", "", $m_place);
还有regexp方法:
$m_place = preg_replace('/,([^,]+)$/','\1',$m_place);
还有regexp方法:
$m_place = preg_replace('/,([^,]+)$/','\1',$m_place);