PHP addslashes and stripslashes implement string conversion and restoration usage instance

Source: Internet
Author: User
: This article mainly introduces examples of character string conversion and restoration usage of addslashes and stripslashes in PHP. For more information about PHP tutorials, see. The example in this article describes how to convert and restore strings using addslashes () and stripslashes () in PHP. We will share this with you for your reference. The details are as follows:

In PHP, the addslashes () function returns a string that adds a backslash before a predefined character.

The predefined characters are:

Single quotes (')
Double quotation marks (")
Backslash (\)
NULL

The stripslashes () function deletes the backslash added by the addslashes () function.

Example:

<? Php $ str = "select * from 'book' where bookname = ''"; echo $ str ."
"; // Output string $ str echo $ astr = addslashes ($ str); // escape string and output echo"
"; Echo stripslashes ($ astr); // restore the escape string?>

The running result is as follows:

Select * from 'book' where bookname = 'site 'select * from 'book' where bookname = \ 'site \ 'select * from 'book' where bookname = 'site'

Supplement:

Differences between addslashes () and addcslashes () functions:

These two functions are very similar in writing but use different functions. the addslashes () function returns a string that adds a backslash before a predefined character, while the addcslashes () function () the function returns a string that adds a backslash before a specified character. Therefore, the addcslashes () function needs to add additional parameters to indicate that the specific character of the backslash needs to be added.

For details about the differences and usage of the two, refer to the article "differences and comparison between parsing php addslashes () and addcslashes () functions".

I hope this article will help you with PHP programming.

The above introduces the examples of character string conversion and restoration usage of addslashes and stripslashes in PHP, including some content, and hope to help friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.