简体   繁体   English

在数组上使用preg_replace

[英]Using preg_replace on a array

I have a relatively large array of elements which I want to search for a string and replace any matches. 我有一个相对较大的元素数组,我想搜索一个字符串并替换任何匹配。 I'm currently trying to do this using preg_replace and regular expressions: 我目前正在尝试使用preg_replace和正则表达式:

preg_replace("/\d?\dIPT\.\w/", "IPT", $array);

I want to get all values which match either 00IPT.A or 0IPT.A (with 0 representing any numerical character and A representing any letter) and replace them with IPT . 我希望获得与00IPT.A0IPT.A匹配的所有值( 0代表任何数字字符, A代表任何字母)并用IPT替换它们。 However, I'm getting array to string conversion notices. 但是,我正在获取数组到字符串转换通知。 Is there any way to get preg_replace to accept an array data source? 有没有办法让preg_replace接受一个数组数据源? If not, is there any other way I could achieve this? 如果没有,还有其他方法可以实现吗?

EDIT: 编辑:

The documentation says that preg_replace should be able to accept array sources — this is the reason I'm asking. 文档说preg_replace应该能够接受数组源 - 这就是我要问的原因。

The string or an array with strings to search and replace. 字符串或包含要搜索和替换的字符串的数组。 If subject is an array, then the search and replace is performed on every entry of subject, and the return value is an array as well. 如果subject是数组,则对主题的每个条目执行搜索和替换,并且返回值也是数组。

The array is multidimensional if that helps (has multiple arrays under one main array). 如果有帮助(在一个主阵列下有多个数组),该数组是多维的。

preg_replace doesn't modify in place. preg_replace不会在适当的位置修改。 To permanently modify $array , you simply need to assign the result of preg_replace to it: 要永久修改$array ,只需将preg_replace的结果赋给它:

$array = preg_replace("/\d{1,2}IPT\.\w/", "IPT", $array);

works for me. 适合我。

$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" }

Note: the \\d{1,2} means one or two digits. 注意: \\d{1,2}表示一位或两位数。

If you want to do this to a two-dimensional array, you need to loop through the first dimension: 如果要对二维数组执行此操作,则需要遍历第一维:

$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" 
    } 
}

Note that you have to loop through each row by reference ( &$row ) otherwise the original array will not be modified. 请注意,您必须通过引用&$row )遍历每一行否则将不会修改原始数组。

your $array contains some further arrays. 你的$array包含一些其他数组。 preg_replace works fine with arrays of strings, but not with arrays of arrays [of arrays...] of strings. preg_replace适用于字符串数组,但不适用于字符串数组[of arrays ...]的数组。

Your value does not sit in the array as a simple element but as a subset right? 您的值不是作为一个简单的元素在数组中,而是作为一个子集对吗? Like so? 像这样?

array (
  array ('id' => 45, 'name' => 'peter', 'whatyouarelookingfor' => '5F'),
  array ('id' => 87, 'name' => 'susan', 'whatyouarelookingfor' => '8C'),
  array ('id' => 92, 'name' => 'frank', 'whatyouarelookingfor' => '9R')
)

if so: 如果是这样:

<?php

foreach ($array as $key => $value) {
  $array[$key]['whatyouarelookingfor'] =  
    preg_replace("/\d?\dIPT\.\w/", "IPT", $value['whatyouarelookingfor']);
}

Or if more elements have this, just go broader: 或者如果有更多的元素,那就更广泛了:

<?php

foreach ($array as $key => $value) {
  $array[$key] =  
    preg_replace("/\d?\dIPT\.\w/", "IPT", $value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM