preg_replace
没有修改到位。永久修改$array
,您只需分配结果preg_replace
to it:
$array = preg_replace("/\d{1,2}IPT\.\w/", "IPT", $array);
对我有用。
$array = array('00IPT.A', '0IPT.A');
$array = preg_replace("/\d{1,2}IPT\.\w/", "IPT", $array);
var_dump($array);
// output: array(2) { [0]=> string(3) "IPT" [1]=> string(3) "IPT" }
注:\d{1,2}
表示一位或两位数字。
如果要对二维数组执行此操作,则需要循环遍历第一个维度:
$array = array( array('00IPT.A', 'notmatch'), array('neither', '0IPT.A') );
foreach ($array as &$row) {
$row = preg_replace("/\d{1,2}IPT\.\w/", "IPT", $row);
}
var_dump($array);
output:
array(2) {
[0]=> array(2) {
[0]=> string(3) "IPT"
[1]=> string(8) "notmatch"
}
[1]=> &array(2) {
[0]=> string(7) "neither"
[1]=> string(3) "IPT"
}
}
请注意,您必须循环遍历每一行引用 (&$row
) 否则原数组将不会被修改。