Home  >  Article  >  Backend Development  >  How to filter special characters in php regularly

How to filter special characters in php regularly

WBOY
WBOYOriginal
2016-07-25 08:54:062831browse
This article introduces the method of regular filtering special characters in PHP programming. Friends in need can refer to it.

To use regular expressions to filter special characters in php, you can use the function preg_quote: string preg_quote( string $str[, string $delimiter] )

Find the special characters that appear in the $str string and belong to the regular expression, and add a backslash in front of the special characters.

An optional $delimiter string parameter can be provided as a new set of escape characters.

Special characters for regular expressions include: . + * ? [ ^ ] $ ( ) { } = ! | : .

string addslashes( string $str )

Add backslashes to special characters in the string to be written to the database. These characters include single quotes (‘), double quotes (“), backslashes () and NULL (NULL character).

When the php.ini command magic_quote_gpc = On, it means that when writing data to the database, special characters in the data will automatically be added with a backslash. You can use the get_magic_quotes_gpc() function to detect whether magic_quote_gpc is turned on. PHP officials recommend setting the value of magic_quote_gpc to Off, because turning on this configuration may cause certain security issues. It is best to manually process the data written to the database.

string addcslashes( string $str, string $charlist )

Escape strings in C style. The $charlist parameter provides the function with a character set that needs to be escaped. That is to say, you can provide the characters you want to escape to the $charlist string parameter. If you want to escape all English letters between a and z, you can write parameters in the form a..z. When using this function, pay attention to the predefined escape sequences in the C language. PHP utility function for filtering special characters Special character filtering method for php form submission html special character filter php class How to escape special characters in url links Detailed explanation of php special character escaping php filter parameter special characters anti-injection php method to filter illegal and special strings Examples of php special character processing functions



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn