当前位置:网站首页>PHP escape string
PHP escape string
2022-07-24 07:21:00 【whatday】
Method 1: Manual escape 、 Restore string
Strings can be in single quotes ' '、 Double quotes " "、 delimiter <<<3 How to define , The simplest way is to use single quotation marks ' ' To define a string . When using strings , It is likely that these symbols that define the string will also be used as part of the string , To avoid confusion , It is necessary to use the transfer character in front of these symbols \.
\ It's an escape sign , Keep up with the \ The next first character will become a meaningless or special character . for example ,' Is a special symbol that defines a string , The written \' after , It loses its function of defining strings , It has become an ordinary single quotation mark . We can use echo "\'"; To output a single quotation mark , Escape character \ Will not show .
【 Example 】 Use escape words \ Escape a string .
<?php
header("Content-type:text/html;charset=utf-8");
echo 'Name:\'php Chinese net \'<br>Url:\'https://www.php.cn//\'';
?>The operation results are as follows :

For simple strings , It is recommended to use manual methods to escape strings , For strings with a large amount of data , It is suggested to use the automatic escape function to escape the string .
2、 Auto escape 、 Restore string
Auto escape 、 Restore string can be used PHP Provided addslashes() Functions and stripslashes() Function implementation .
addslashes() The function adds \, Escape the specified string , The syntax is as follows :
addslashes($str)addslashes() Function returns a string , For the needs of database query and other statements , Some characters will be preceded by a backslash , These characters are single quotes '、 Double quotes "、 Backslash \ And NULL.
stripslashes() The function restores an escaped string , That is to remove the backslash added to the string , The syntax is as follows :
stripslashes($str)stripslashes() Function will return a string after removing the escape backslash (\' Convert to ', Double backslash \\ Converted to a single backslash \).
【 Example 】 Use addslashes() Function to escape a string , And then use stripslashes() Function to restore .
<?php
header("Content-type:text/html;charset=utf-8");
$sql = "select * from php where website='php Chinese net '";
$str = addslashes($sql);
echo $str.'<br>';
$str = stripslashes($str);
echo $str;
?>The operation results are as follows :

边栏推荐
猜你喜欢

17. What is the situation of using ArrayList or LinkedList?

C language start

Aggregated new ecological model - sharing purchase, membership and reward system

Redis fragment cluster

Part I - Fundamentals of C language_ 11. Comprehensive project - greedy snake

MITRE ATT&CK超详细学习笔记-01(背景,术语,案例)

我的创作纪念日

Part II - C language improvement_ 3. Pointer reinforcement

Variables and data types (04) end

Variables and data types (03)
随机推荐
Flow control statement of avascript
Can recursion still play like this? Recursive implementation of minesweeping game
第一部分—C语言基础篇_11. 综合项目-贪吃蛇
Hackingtool of security tools
全国职业院校技能大赛网络安全B模块 缓冲区溢出漏洞
Opencascade notes: GP package
avaScript的流程控制语句
记账APP:小哈记账1——欢迎页的制作
Filter filter
MITRE ATT&CK超详细学习笔记-02(大量案例)
django.db.utils.OperationalError: (2002, “Can‘t connect to local MySQL server through socket ‘/var/r
[steering wheel] the super favorite idea efficiency artifact save actions is uninstalled
Using depth and normal textures in unity
从CIA看常见网络攻击(爆破,PE,流量攻击)
C language from entry to Earth - array
MySql的DDL和DML和DQL的基本语法
第二部分—C语言提高篇_4. 二级指针
【Word】如何生成左侧的索引目录
单场GMV翻了100倍,冷门品牌崛起背后的“通用法则”是什么?
[leetcode] 444. Sequence reconstruction