我现遇到一个问题,在提交表单时,对提交的内容要过滤掉特殊字符如:~!@#$%^&*()_+ ..这些
如我有一提交内容:生活*&)可以)(_更美的如何过滤成:生活可以更美的。我的做法是:
$str=str_replace("&","",$str);  
$str=str_replace(">","",$str);  
$str=str_replace("<","",$str);
$str=str_replace("=","",$str);
$str=str_replace("(","",$str);
$str=str_replace(")","",$str);
$str=str_replace("[","",$str);
$str=str_replace("]","",$str);
$str=str_replace(".","",$str);
$str=str_replace("*","",$str);
$str=str_replace("#","",$str);
$str=str_replace("$","",$str);
$str=str_replace("@","",$str);
$str=str_replace("-","",$str);
$str=str_replace("+","",$str);
$str=str_replace("&","",$str);  
$str=str_replace("!","",$str); 
$str=str_replace("~","",$str); 
$str=str_replace("^","",$str);
$str=str_replace("%","",$str);
$str=str_replace("'","",$str);
$str=str_replace("\"","",$str);有没有更简便的方法。。求指教。。我这有80分。。

解决方案 »

  1.   

    为何:$str=preg_replace("[`~!@#$%^&*()+=|{}':;',//[//].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]",'',$str);这样写,不行呢?
      

  2.   

    $p = str_split("&><=()[].*#$@-+&! ~ ^%'\"_");
    $s = '生活*&)可以)(_更美的';
    echo str_replace($p, '', $s);生活可以更美的
      

  3.   


    全角的话改成用下面的函数<?php 
    function mb_str_split( $string, $encoding='UTF-8' ) { 
        # Split at all position not after the start: ^ 
        # and not before the end: $ 
        mb_regex_encoding($encoding);
        mb_internal_encoding($encoding); 
        return preg_split('/(?<!^)(?!$)/u', $string, ); 
    } $string   = '火车票'; 
    $charlist = mb_str_split( $string ); 
    // 如果是gbk的话使用
    // $charlist = mb_str_split( $string,'GBK' ); 
    print_r( $charlist ); 
    ?> # Prints: 
    Array 

        [0] => 火 
        [1] => 车 
        [2] => 票 
    )