我现遇到一个问题,在提交表单时,对提交的内容要过滤掉特殊字符如:~!@#$%^&*()_+ ..这些
如我有一提交内容:生活*&)可以)(_更美的
如何过滤成:生活可以更美的。
我的做法是:
$str=str_replace("&","",$str);
$str=str_replace(">","",$str);
$str=str_replace(" $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分。。
为何:$str=preg_replace("[`~!@#$%^&*()+=|{}':;',//[//]./?~!@#¥%……&*()??+|{}【】‘;:”“’。,、?]",'',$str);这样写,不行呢?
为何:$str=preg_replace("[`~!@#$%^&*()+=|{}':;',//[//]./?~!@#¥%……&*()??+|{}【】‘;:”“’。,、?]",'',$str);这样写,不行呢?
1.最大问题是正则没有边界符
2.转义不是//而是,双引号内是\
3.|被看作是或选择了
4.双引号内还有双引号么
……
大哥,那要怎么弄才行呀。
$p = str_split("&><=()[].*#$@-+&! ~ ^%'"_");$s = '生活*&)可以)(_更美的';echo str_replace($p, '', $s);明白了,多谢指点...只是不知全角的特符字符如¥)(,这些是否也能适用。。我试下
明白了,多谢指点...只是不知全角的特符字符如¥)(,这些是否也能适用。。我试下
全角的话改成用下面的函数
<?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] => 票 ) 对于切割串为数组,全角不适合
但手工书写的数组就是一样的了
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号