一个阵列上使用preg_replace [英] Using preg_replace on a array

查看:179
本文介绍了一个阵列上使用preg_replace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个比较大的阵列,我想搜索字符串和替换任何匹配的元素。目前我正在努力做到这一点使用 preg_replace 和定期EX pressions:

  preg_replace(/ \\ D \\ DIPT \\ \\ W /?,IPT,$阵列);

我想匹配任何一种 00IPT.A 0IPT.A (用<$ C,所有值$ C> 0 重新presenting任何数字字符和 A 重新presenting任何字母),并取代它们与 IPT 。但是,我越来越数组字符串的转换通知。有没有什么办法让 preg_replace 来接受一个数组数据源?如果没有,是否有任何其他方式,我可以做到这一点?

编辑:

文档说 preg_replace 应该能够接受阵源 - 这是我在问其原因


  

的字符串或字符串数​​组搜索和替换。
  如果对象是一个数组,然后搜索和替换是在主题的每个条目执行,并且返回值是一个数组为好。


数组是多维的,是否可以帮助(在一个主阵列具有多个阵列)。


解决方案

preg_replace 到位不修改。要永久修改 $阵列,你只需要指定 preg_replace 的结果是:

  $阵列= preg_replace(/ \\ D {1,2} IPT \\ \\ W /,IPT,$阵列);

我的作品。

  $阵列=阵列('00IPT.A','0IPT.A');
$阵列= preg_replace(/ \\ D {1,2} IPT \\ \\ W /,IPT,$阵列);
后续代码var_dump($数组);
//输出:阵列(2){[0] =&GT;串(3)IPT[1] =&GT;串(3)IPT}

注:在 \\ D {1,2} 是指一个或两个数字

如果你想要做这一个二维数组,您可以通过第一个维度需要循环:

  $阵列=阵列(阵列('00IPT.A','notmatch'),阵列('没有','0IPT.A'));
的foreach($数组和放大器; $行){
    $行= preg_replace(/ \\ D {1,2} IPT \\ \\ W /,IPT,$行);
}
后续代码var_dump($数组);

输出:

 阵列(2){
    [0] =&GT;阵列(2){
        [0] =&GT;串(3)IPT
        [1] =&GT;串(8)notmatch
    }
    [1] =&GT; &安培;阵列(2){
        [0] =&GT;串(7)两者都不
        [1] =&GT;串(3)IPT
    }
}

请注意,您引用的每一行的必须循环&放大器; $行),否则原始数组不会被修改<。 / p>

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("/\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. However, I'm getting array to string conversion notices. Is there any way to get preg_replace to accept an array data source? 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.

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.

The array is multidimensional if that helps (has multiple arrays under one main array).

解决方案

preg_replace doesn't modify in place. To permanently modify $array, you simply need to assign the result of preg_replace to it:

$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.

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.

这篇关于一个阵列上使用preg_replace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆