preg_replace在数组php中

时间:2015-04-27 13:32:02

标签: php mysqli replace preg-replace str-replace

$search = array("<?php", "god", "gOd"); //do not want to do this in so many words\\
$replace = array("<-php", "God", "God");
$comment = str_replace($search, $replace, mysqli_real_escape_string($conexao, $_POST['comment']));

我想在数组中包含大写和小写字母,如何为$search$replace执行此操作?

2 个答案:

答案 0 :(得分:0)

如果必须使用正则表达式,请编写如下代码:

preg_replace('/regular expression here/i', 'replacement here', $string);

否则使用str_ireplace()http://php.net/manual/en/function.str-ireplace.php

答案 1 :(得分:0)

<?php
$db = new mysqli('localhost', 'root');
$a = array("<?php", "god", "gOd");
foreach ($a as &$v) {
    $v = mysqli_real_escape_string(
        $db,
        preg_replace("/[^A-Za-z0-9?!]/", '', $v)
    );
}
var_export($a);

结果:

array (
  0 => '?php',
  1 => 'god',
  2 => 'gOd',
)