How to escape and anti-escape characters in php

青灯夜游
Release: 2023-03-15 11:02:01
Original
3228 people have browsed it

In PHP, you can use the addslashes() function to escape characters, the syntax is "addslashes($str)"; you can use the stripslashes() function to reverse escape characters and restore an escaped character. A defined string with the syntax "stripslashes($str)".

"How

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In PHP, you can use the addslashes() function and stripslashes() function to realize character escaping and anti-escaping.

1. addslashes() function

addslashes() function is to add \ to the string and convert the specified string. Definition, the syntax format is as follows:

addslashes($str)
Copy after login

Among them, $str is the string to be escaped.

In the string returned by the addslashes() function, for the purpose of database queries and other statements, backslashes are added before certain characters. These characters are single quotes ', Double quotes ", backslashes \ and NULL.

Example:

<?php 
header("Content-type:text/html;charset=utf-8");
$sql = "select * from php where website=&#39;PHP中文网&#39;";
$str = addslashes($sql);
echo($str); 
?>
Copy after login

"How

2. Stripslashes() function

stripslashes() function is to restore an escaped string, that is, to remove the backslash added to the string. The syntax format is as follows:

stripslashes($str)
Copy after login

Among them, $str is the string that needs to be restored.

stripslashes() function will return a character after removing the escaped backslash String (\' is converted to ', double backslash \\ is converted to single backslash \).

Example:

<?php 
header("Content-type:text/html;charset=utf-8");
$sql = "select * from php where website=\&#39;PHP中文网\&#39;";
$str = stripslashes($sql);
echo($str); 
?>
Copy after login

"How

Recommended learning: "PHP Video Tutorial"

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

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 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!