删除逗号分隔字符串中的字符 php
remove character in comma separated string php
这是我的字符串:
$codes = 60textone_13, 120texttwo_14, 60textthree_15, 90textfour_16,
我删除了最后一个逗号,例如:
$implode_comma = implode(', ', $codes);
我正在尝试删除“_number”,所以我希望我的字符串为:
$codes = 60textone, 120texttwo, 60textthree, 90textfour
我尝试删除“_number”:
$variable = substr($implode_comma, 0, strpos($implode_comma, "_"));
但它只有returns第一个元素:
60textone
我该如何解决?谢谢。
如果 $codes
是一个字符串,您可以将正则表达式与 preg_replace()
一起使用:
$codes = "60textone_12, 120texttwo_13, 60textthree_14, 90textfour_15";
$no_number = preg_replace('/_\d+/', '', $codes);
echo $no_number;
如果 $codes
是一个数组,您将遍历它们,使用 preg_replace
将 _number
与正则表达式 /_\d+/
:
匹配
$codes = array("60textone_13", "120texttwo_14", "60textthree_15", "90textfour_16");
foreach($codes AS $code) {
$new_code[] = preg_replace('/_\d+/', '', $code);
}
echo implode(',', $new_code);
正则表达式的解释:
第 1 个捕获组 (_\d+)
_
字面上匹配字符 _(区分大小写)
\d
匹配一个数字(等于[0-9])
+
量词——匹配一次到无限次,尽可能多次,按需回馈(贪心)
此处:
<?php
$codes = '60textone_13, 120texttwo_14, 60textthree_15, 90textfour_16';
$codes = explode(', ', $codes);
$result = [];
foreach ($codes as $code) {
$result[] = preg_replace('/(_)\w+/', '', $code);
}
var_dump($result);
?>
输出:
array(4) { [0]=> string(9) "60textone" [1]=> string(10) "120texttwo" [2]=> string(11) "60textthree" [3]=> string(10) "90textfour" }
如果你想要字符串而不是数组,你可以内爆你的数组,只需在 var_dump($result);
之前添加这段代码
$result = (implode(', ', $result));
试试这个
$str ="60textone_13,120texttwo_14,60textthree_15, 90textfour_16";
$codes = explode(',', $str);
foreach ($codes as $value) {
$variable[] = substr($value, 0, strpos($value, "_"));
}
$implode_comma = implode(',',$variable);
echo $implode_comma;
假设您的 $codes
是一个类似这样的字符串:60textone_13,120texttwo_14,60textthree_15,90textfour_16
(如果不是,请查看答案的末尾如何做到这一点**)。
现在您可以像这样使用 array-map:
$arr = explode(",",trim($str));
function removeNum($s) {
return substr($s, 0, -3);
}
$a = array_map("removeNum", $arr);
echo print_r($a, true);
如果号码不总是 2 位数字,请使用:
substr($s, 0, strpos($s, "_"));
输出:
Array (
[0] => 60textone
[1] => 120texttwo
[2] => 60textthree
[3] => 90textfour
)
**如果不使用下面的代码:
$codes = "60textone_13, 120texttwo_14, 60textthree_15, 90textfour_16,";
$str=preg_replace('/\s+/', '', rtrim($codes,",")); //remove spaces and last comma
这是我的字符串:
$codes = 60textone_13, 120texttwo_14, 60textthree_15, 90textfour_16,
我删除了最后一个逗号,例如:
$implode_comma = implode(', ', $codes);
我正在尝试删除“_number”,所以我希望我的字符串为:
$codes = 60textone, 120texttwo, 60textthree, 90textfour
我尝试删除“_number”:
$variable = substr($implode_comma, 0, strpos($implode_comma, "_"));
但它只有returns第一个元素:
60textone
我该如何解决?谢谢。
如果 $codes
是一个字符串,您可以将正则表达式与 preg_replace()
一起使用:
$codes = "60textone_12, 120texttwo_13, 60textthree_14, 90textfour_15";
$no_number = preg_replace('/_\d+/', '', $codes);
echo $no_number;
如果 $codes
是一个数组,您将遍历它们,使用 preg_replace
将 _number
与正则表达式 /_\d+/
:
$codes = array("60textone_13", "120texttwo_14", "60textthree_15", "90textfour_16");
foreach($codes AS $code) {
$new_code[] = preg_replace('/_\d+/', '', $code);
}
echo implode(',', $new_code);
正则表达式的解释:
第 1 个捕获组 (_\d+)
_
字面上匹配字符 _(区分大小写)\d
匹配一个数字(等于[0-9])+
量词——匹配一次到无限次,尽可能多次,按需回馈(贪心)
此处:
<?php
$codes = '60textone_13, 120texttwo_14, 60textthree_15, 90textfour_16';
$codes = explode(', ', $codes);
$result = [];
foreach ($codes as $code) {
$result[] = preg_replace('/(_)\w+/', '', $code);
}
var_dump($result);
?>
输出:
array(4) { [0]=> string(9) "60textone" [1]=> string(10) "120texttwo" [2]=> string(11) "60textthree" [3]=> string(10) "90textfour" }
如果你想要字符串而不是数组,你可以内爆你的数组,只需在 var_dump($result);
$result = (implode(', ', $result));
试试这个
$str ="60textone_13,120texttwo_14,60textthree_15, 90textfour_16";
$codes = explode(',', $str);
foreach ($codes as $value) {
$variable[] = substr($value, 0, strpos($value, "_"));
}
$implode_comma = implode(',',$variable);
echo $implode_comma;
假设您的 $codes
是一个类似这样的字符串:60textone_13,120texttwo_14,60textthree_15,90textfour_16
(如果不是,请查看答案的末尾如何做到这一点**)。
现在您可以像这样使用 array-map:
$arr = explode(",",trim($str));
function removeNum($s) {
return substr($s, 0, -3);
}
$a = array_map("removeNum", $arr);
echo print_r($a, true);
如果号码不总是 2 位数字,请使用:
substr($s, 0, strpos($s, "_"));
输出:
Array (
[0] => 60textone
[1] => 120texttwo
[2] => 60textthree
[3] => 90textfour
)
**如果不使用下面的代码:
$codes = "60textone_13, 120texttwo_14, 60textthree_15, 90textfour_16,";
$str=preg_replace('/\s+/', '', rtrim($codes,",")); //remove spaces and last comma