dtrnish3637
2015-04-27 13:32 阅读 64
已采纳

preg_replace在数组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']));

I want to include uppercase and lowercase letters in the array, how to do this for $search and $replace?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    douya6229 douya6229 2015-04-27 13:35

    If you have to use regular expressions, write your code like this:

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

    Otherwise use str_ireplace(): http://php.net/manual/en/function.str-ireplace.php

    点赞 评论 复制链接分享
  • dongzuan4917 dongzuan4917 2015-04-27 13:42
    <?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);
    

    Result:

    array (
      0 => '?php',
      1 => 'god',
      2 => 'gOd',
    )
    
    点赞 评论 复制链接分享

相关推荐