Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does php escape a string into a special character

Shulou Source: shulou.com Published: 2022-05-31 10:56:26 09月14日 Update

This article introduces the relevant knowledge of "how php escapes strings into special characters". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Escape character description

In double quotation marks, all escape characters can be used normally.

In single quotation marks, only single quote escape characters can be used ("), and nothing else can be used.

2. Using mysql library function

The PHP version is prior to 7.0:

Mysql_escape_string (string $unescaped_string): string

The PHP version is later than 7.0:

Mysqli_real_escape_string (mysqli $link, string $escapestr): string

3. Using the escape function addslashes ()

Suitable for version PHP4, PHP5, PHP7

Addslashes (string $str): string

Before PHP 5.4.The PHP instruction magic_quotes_gpc defaults to on, and virtually all GET, POST, and COOKIE data are used by addslashes (). Do not use addslashes () on strings that have already been escaped by magic_quotes_gpc, as this will result in double-layer escape. You can use the function get_magic_quotes_gpc () to detect when you encounter this situation. That is, when get_magic_quotes_gpc () returns false, addslashes () is used for special character escape. Examples are as follows:

Function myaddslashes ($data) {if (false = = get_magic_quotes_gpc ()) {return addslashes ($data); / / when magic reference is not enabled, escape special characters} return $data;} "how php escapes strings into special characters" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Characters escape special string function quotation mark version content actual situation more knowledge practical successful learning and then double layer only dilemma actually instruction Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Linux vpn Docker MariaDB