Home > php教程 > php手册 > body text

实例详解PHP简单字符串的过滤方法

PHPz
Release: 2018-10-22 17:24:56
Original
1209 people have browsed it

这篇文章主要介绍了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>
Copy after login

 效果图如下:

 【相关教程推荐】

1. php编程从入门到精通全套视频教程
2. php从入门到精通 
3. bootstrap教程

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!