preg_replace 数组 php
preg_replace in array php
$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
如何做到这一点?
如果您必须使用正则表达式,请按如下方式编写代码:
preg_replace('/regular expression here/i', 'replacement here', $string);
否则使用str_ireplace()
:
http://php.net/manual/en/function.str-ireplace.php
<?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',
)
$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
如何做到这一点?
如果您必须使用正则表达式,请按如下方式编写代码:
preg_replace('/regular expression here/i', 'replacement here', $string);
否则使用str_ireplace()
:
http://php.net/manual/en/function.str-ireplace.php
<?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',
)