Home > Backend Development > PHP Problem > How to remove illegal characters in php

How to remove illegal characters in php

王林
Release: 2023-03-08 07:38:01
Original
3122 people have browsed it

php去除非法字符的方法:可以利用php中的str_replace函数来去除字符串中的非法字符,如【$str = str_replace('\\','',$str);】。

How to remove illegal characters in php

本文操作环境:windows10系统、php 7.3、thinkpad t480电脑。

函数介绍:

str_replace() 函数替换字符串中的一些字符(区分大小写)。

语法:

str_replace(find,replace,string,count)
Copy after login

参数介绍:

  • find 必需。规定要查找的值。

  • replace 必需。规定替换 find 中的值的值。

  • string 必需。规定被搜索的字符串。

  • count 可选。一个变量,对替换数进行计数。

实现代码:

/**
 * 去掉字符串里非法的字符
 * @param $str string
 * @return string
 */
function remove_chars($str)
{
    $str = str_replace('\\','',$str);
    $str = str_replace('/','',$str);
    $str = str_replace('*','',$str);
    $str = str_replace('?','',$str);
    $str = str_replace(':','',$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);
    return $str;
}
Copy after login

相关推荐:php教程

The above is the detailed content of How to remove illegal characters in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template