Analysis of addcslashes and stripcslashes functions in PHP

Source: Internet
Author: User
This article mainly introduces the usage of addcslashes and stripcslashes functions in PHP, and analyzes the techniques related to addcslashes function addition for fixed character escape processing and stripcslashes restoration, for more information about addcslashes and stripcslashes functions in PHP, see the examples in this article. We will share this with you for your reference. The details are as follows:

When I write an English version of a website, I fill in the English document after writing it. I don't have any questions when I fill it out. However, when I fill in the specified content, I cannot fill it in, and no error is reported, I checked the database and found that this field uses the "TEXT" data type. I thought it was because the content was too long, so I changed the data type to "longtext ", however, the same problem still occurs during submission. Next, let's introduce the addcslashes function!

Later, I asked my colleagues to find out why the punctuation marks "'" are in English. After MySQL runs here, it automatically considers the statement to be over, so I cannot fill it out. Now that the problem is identified, you have to find the corresponding solution, that is, add the escape character "" before "'" in the text content. in PHP, the addcslashes and stripcslashes functions that automatically add or remove escape characters to strings are provided. after the test is added, the problem is solved! It can be seen that I do not strictly prohibit writing programs on weekdays, and I will always ignore such details. If HACKER finds these problems and uses them one by one, the website will basically be OVER, so everyone must take the precaution, don't make the same mistake as me.

The following describes the usage of these two functions:

String addcslashes (string str, string charlist)

The first parameter 'str' is the original string to be lost.

The charlist parameter contains the character "" before the character of the original string.

String stripcslashes (string str)

Remove "" from the string.

In addition, the addslashes function can also be used for escape processing.

Example:

<?php$sql = "update book set bookname='let's go' where bookid=1"; echo $sql."
"; $new_sql = addcslashes($sql,"'"); echo $new_sql."
"; $new_sql_01 = stripcslashes($new_sql); echo $new_sql_01."
"; echo addslashes($sql);?>

The running result is as follows:

update book set bookname='let's go' where bookid=1update book set bookname=\'let\'s go\' where bookid=1update book set bookname='let's go' where bookid=1update book set bookname=\'let\'s go\' where bookid=1

I hope this article will help you with PHP programming.

Related Article

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.