Home > Backend Development > PHP Tutorial > How to filter user input data in PHP? (code example)

How to filter user input data in PHP? (code example)

不言
Release: 2023-04-04 16:28:01
forward
3699 people have browsed it

本篇文章给大家带来的内容是关于PHP用户输入数据如何进行过滤?(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

1、在表单中,input项,如果用户输入的是英文状态下的双引号或单引号,数据保存后。以后又在后台编辑的时候,因为双引号或单引号的原因,发现数据“丢失”。

2、因此要将输入数据中引号变成html实体。

3、怎么变?答曰:htmlentities

    //php 5.2.6
    $text = trim($text);//去除数据头尾空格
    //$text = strip_tags($text); //去掉html标签,根据情况决定是否用此
    $text = htmlentities($text,ENT_QUOTES);//单引号,双引号都转化
Copy after login

4、发现出来的数据是乱码!

5、原来,虽然htmlentities是默认UTF-8,但是还是要加上。

    $text = trim($text);    
    //$text = strip_tags($text);
    $text = htmlentities($text,ENT_QUOTES,"UTF-8");
Copy after login

6、嗯,正常了,英文双引号变成了"   <变成了<等

7、对用户数据再次编辑的时候,直接从数据库读出来,塞给input的value即可,不需要转码处理之后再塞给input。

The above is the detailed content of How to filter user input data in PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:cnblogs.com
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template