Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 删除strpo_Php - Fatal编程技术网

Php 删除strpo

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

如何剥离最后一个逗号(第二个逗号),我知道如何使用strpos定位它,但不确定如何删除它

$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);