php过滤特殊危险字符的总结(php正则过滤特殊字符)

发布时间:2022-11-15

本文目录一览:

  1. PHP怎样过滤中文状态下特殊字符(比如标点符号)?
  2. php 如何过滤特殊字符,如 ◆ )- : 、 、!! /   等
  3. PHP字符串中特殊符号的过滤方法介绍
  4. php怎样过滤掉特殊字符啊 ☺

PHP怎样过滤中文状态下特殊字符(比如标点符号)?

function filterGBK_SpecialChars($str)
{
    $str = urlencode($str); //将关键字编码
    //下面的必须写在一行,不可换行截断
    $str = preg_replace("/(%7E|%60|%21|%40|%23|%24|%25|%5E|%26|%27|%2A|%28|%29|%2B|%7C|%5C|%3D|\-|_|%5B|%5D|%7D|%7B|%3B|%22|%3A|%3F|%3E|%3C|%2C|\.|%2F|%A3%BF|%A1%B7|%A1%B6|%A1%A2|%A1%A3|%A3%AC|%7D|%A1%B0|%A3%BA|%A3%BB|%A1%AE|%A1%AF|%A1%B1|%A3%FC|%A3%BD|%A1%AA|%A3%A9|%A3%A8|%A1%AD|%A3%A4|%A1%A4|%A3%A1|%A1%AB|%A3%FB|%A3%FD|%A1%BE|%A1%BF|)+/", '', $str);
    $str = urldecode($str); //将过滤后的关键字解码
    return $str;
}
$str = '广~·@#¥%……*()——+|-=、{}【】:;“”‘’~“《》,。?、州;?海【,鲜。餐“”】(,厅) ';
echo filterGBK_SpecialChars($str);

php 如何过滤特殊字符,如 ◆ )- : 、 、!! /   等

可以使用 str_replace() 函数统一替换,例如:

$string = "测试◆例子♂ 在此 !";
$replace = array('◆','♂',')','=','+','$','¥','-','、','、',':',';','!','!','/');
$string = str_replace($replace, '', $string);
echo $string;

PHP字符串中特殊符号的过滤方法介绍

本篇文章主要是对PHP字符串中特殊符号的过滤方法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助。 有时候我们会遇到过滤字符串中特殊字符的问题,本文提供了一个处理特殊字符串的方法,可能有遗漏,如果读者发现了可以补充。 代码如下:

function strFilter($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);
    $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);
    $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);
    return trim($str);
}

php怎样过滤掉特殊字符啊 ☺

过滤掉特殊字符,可以考虑使用字符串替换的方法,在PHP中替换字符效率最高也是最简单字符替换函数 str_replace 函数。

使用方法:

str_replace(find, replace, string, count)

参数说明:

  • find 必需。规定要查找的值。
  • replace 必需。规定替换 find 中的值的值。
  • string 必需。规定被搜索的字符串。
  • count 可选。一个变量,对替换数进行计数。

实例:

str_replace("iwind", "kiki", "i love iwind, iwind said");

将输出:

i love kiki, kiki said

当然你也可以采取正则替换的方法,该函数是 preg_replace