Home >Backend Development >PHP Tutorial >PHP simple string filtering method example sharing

PHP simple string filtering method example sharing

墨辰丷
墨辰丷Original
2018-06-01 14:08:261899browse

这篇文章主要介绍了PHP简单字符串过滤方法,结合实例形式分析了php通过对字符串中特殊字符进行正则替换实现字符过滤功能的简单操作技巧,需要的朋友可以参考下

具体如下:


<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
<title>PHP字符串的过滤方法</title>
</head>
<body>
<?php
function strFilter($str){
  //特殊字符的过滤方法
  $str = str_replace(&#39;`&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;·&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;~&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;!&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;!&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;@&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;#&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;$&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;¥&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;%&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;^&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;……&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;&&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;*&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;(&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;)&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;(&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;)&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;-&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;_&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;——&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;+&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;=&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;|&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;\\&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;[&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;]&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;【&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;】&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;{&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;}&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;;&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;;&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;:&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;:&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;\&#39;&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;"&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;“&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;”&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;,&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;,&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;<&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;>&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;《&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;》&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;.&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;。&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;/&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;、&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;?&#39;, &#39;&#39;, $str);
  $str = str_replace(&#39;?&#39;, &#39;&#39;, $str);
  //防sql防注入代码的过滤方法
  $str = str_replace(&#39;and&#39;,&#39;&#39;,$str);
  $str = str_replace(&#39;execute&#39;,&#39;&#39;,$str);
  $str = str_replace(&#39;update&#39;,&#39;&#39;,$str);
  $str = str_replace(&#39;count&#39;,&#39;&#39;,$str);
  $str = str_replace(&#39;chr&#39;,&#39;&#39;,$str);
  $str = str_replace(&#39;mid&#39;,&#39;&#39;,$str);
  $str = str_replace(&#39;master&#39;,&#39;&#39;,$str);
  $str = str_replace(&#39;truncate&#39;,&#39;&#39;,$str);
  $str = str_replace(&#39;char&#39;,&#39;&#39;,$str);
  $str = str_replace(&#39;declare&#39;,&#39;&#39;,$str);
  $str = str_replace(&#39;select&#39;,&#39;&#39;,$str);
  $str = str_replace(&#39;create&#39;,&#39;&#39;,$str);
  $str = str_replace(&#39;delete&#39;,&#39;&#39;,$str);
  $str = str_replace(&#39;insert&#39;,&#39;&#39;,$str);
  $str = str_replace(&#39;or&#39;,&#39;&#39;,$str);
  return trim($str);
}
$cont = &#39;  ?”?;onestopweb.cn and update //\ as chaoyi 》、  &#39;;
echo &#39;开始[&#39;.strFilter($cont).&#39;]结束&#39;;
?>
</body>
</html>


效果图如下:


总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

php插入含有特殊符号数据的处理方法

php监测数据是否成功插入到Mysql数据库的方法

php实现的网页版剪刀石头布游戏

The above is the detailed content of PHP simple string filtering method example sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn