Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of escaping a string in PHP

Shulou Source: shulou.com Published: 2022-06-02 18:14:28 10月01日 Update

This article introduces the knowledge of "what is the method of escaping strings in PHP". 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!

Escape character-manually escape string

In the previous article, we introduced escape characters, which can be thought of as special characters that can be escaped. Escape characters usually start with a backslash "\", followed by one or more characters. In this way, the escape character has a meaning that can be escaped. Simply put, escape characters are treated as a special character constant.

Escape characters can convert ordinary characters into special uses, and with these special purposes, characters can be transformed into their original meaning.

If you want to use escape characters to convert a string, it should be noted that if a string is defined in single or double quotes, if there are characters in the string that can interfere with the definition of the string, at this time, we need to escape the characters that may have interference in the string, so that we can avoid ambiguity in the definition of conceit.

Next, let's take a look at escaping a string by escaping characters through an example, as follows:

When we want to output: URL: 'https://www.yisu.com/', if we simply use echo to output a string, the characters in it may affect the final output and cause errors. This problem can be avoided by escaping characters:

Output result:

At this point, we escape the string through escape characters, so that we can output the results we want.

This kind of manual escape is suitable for some simple strings, when the string is very complex, or the string is very long, it is troublesome to use escape characters for manual escape, is there any good solution?

The addslashes () function and the stripslashes () function are provided in PHP to automatically escape strings, so let's take a look.

Addslashes () function-automatically escapes strings

The addslashes () function provided in PHP is used for automatic escape. Through the addslashes () function, you can append a backslash "\" before a predefined character in a string, where the predefined character includes single quotation marks, double quotes, and backslash.

The basic syntax format of this function is as follows:

Addslashes (string)

It is important to note that the parameter string represents the string that needs to be escaped, and the result returned is the string that has been escaped.

Let's take a look at the use of this function through an example, as follows:

Tags: Characters escapes strings functions that is slashes results output examples special quotation marks methods and then in this case manual transit life longer than Nanshan happiness such as Donghai every day up content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MySQL Shulou Technology Apple Huawei