Several data processing functions in PHP

Source: Internet
Author: User
Recently, I encountered several PHP string processing functions in processing the input data of the front-end form. I really don't know what the function is. The API is too vague to understand, please help me to answer the difference and functions: htmlspecialcharsaddslashesstripslashesstrip_tags... recently, I encountered several PHP string processing functions in processing the input data of the front-end form. I really don't know what the function is. The API is too vague to understand, please help me to answer the differences and functions:

  • Htmlspecialchars

  • Addslashes

  • Stripslashes

  • Strip_tags

  • Nl2br

  • Mysql_real_escape_string

I just want to insert the data in textarea into the mysql database. Users may enter html and other messy strings and insert them into mysql. What should I do?

Reply content:

Recently, I encountered several PHP string processing functions in processing the input data of the front-end form. I really don't know what the function is. The API is too vague to understand, please help me to answer the differences and functions:

  • Htmlspecialchars

  • Addslashes

  • Stripslashes

  • Strip_tags

  • Nl2br

  • Mysql_real_escape_string

I just want to insert the data in textarea into the mysql database. Users may enter html and other messy strings and insert them into mysql. What should I do?

Standard writing (please forget the mess of Functions ):

// Check the function MySQLCheck (& $ value) that prevents SQL Injection {// remove the slash (the escape slash given by the server configuration) if (get_magic_quotes_gpc ()) {$ value = stripslashes ($ value);} // if it is not a number, enclose it with quotation marks (professional escape Function) if (! Is_numeric ($ value) {$ value = "'". mysql_real_escape_string ($ value ). "'";} return $ value; // example usage: // $ user = SQLCheck ($ _ POST ['user']); // $ pwd = SQLCheck ($ _ POST ['pwd']); // $ SQL = "SELECT * FROM users WHERE user = $ user AND password = $ pwd ";}

Function source (Open Source Library): https://github.com/MoonLord-LM/MyPHP
In addition, the new version of PHP has abandoned mysql extension, and mysqli extension is recommended. The Code is as follows:

// Check function MySQLCheck (& $ value) {global $ Connect; // remove the slash (Escape slash given by server configuration) if (get_magic_quotes_gpc ()) {$ value = stripslashes ($ value);} // if it is not a number, enclose it with quotation marks (professional escape Function) if (! Is_numeric ($ value) {$ value = "'". mysqli_real_escape_string ($ Connect, $ value ). "'";} return $ value; // example usage: // $ user = SQLCheck ($ _ POST ['user']); // $ pwd = SQLCheck ($ _ POST ['pwd']); // $ SQL = "SELECT * FROM users WHERE user = $ user AND password = $ pwd ";}

The above $ Connect is a database connection.
Note that both functions can be executed only after they are connected to the MySQL database. Otherwise, an error is reported.
In addition, if the front-end is to be displayed, the script and other labels should also be processed. You can use the str_replace function of PHP to replace the labels.

And so on.

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.