我在一个名为$where:的字符串上使用PHPpreg_replace函数
$where = preg_replace( '/data'./', '', $where );
然而,如果$where是一个未知大小的数组,我希望实现相同的表达式替换。
我必须设置一个循环吗?或者有PHP函数可以帮我吗?
如有任何帮助,我们将不胜感激。
非常感谢
好的,这是我的输入数组:
array
0 => null
1 => null
2 =>
array
'condition' =>
array
'column' => string 'start' (length=5)
'operator' => string '>=' (length=2)
'argvalue' => string '2013-11-21 00:00:00' (length=19)
3 =>
array
'condition' =>
array
'column' => string 'start' (length=5)
'operator' => string '<=' (length=2)
'argvalue' => string '2013-11-21 23:59:59' (length=19)
4 => null
这是我的操作:
$where = preg_replace('/start'./', 'Alan', $where );
这是我的输出:
array
0 => string '' (length=0)
1 => string '' (length=0)
2 => string 'Array' (length=5)
3 => string 'Array' (length=5)
4 => string '' (length=0)
这将返回一个数组到字符串的转换错误。
感谢
关于preg_replace
的主题参数的PHP文档
主题
要搜索和替换的字符串或包含字符串的数组。
如果subject是一个数组,则对subject的每个条目执行搜索和替换,返回值也是一个数组。
试试这个代码:
$text = array( "editpostiddata" ,"editpodatastcat", "ch114","ch112");
$result = preg_replace(array_fill(0, 4, "/data/i"),'', $text);
print_r($result);