[Conclusion + discussion] filter various function applications using php strings

Source: Internet
Author: User
[Conclusion + discussion] php string filtering function applications should first review themselves .. because regular expressions are not enough, the filtering conditions written by myself basically fail... I cannot understand the functions I checked online, and an error occurred when I changed them .... under the guidance of various gods of csdn, I opened a little bit of tricks... links to the original question Post are now releasing functions of various paths for comrades who are already dead .... I hope you can add some suggestions and post the commonly used methods to [conclusion + discussion] filter various function applications using php strings.
First, I will review myself. because regular expressions are not enough, the filtering conditions I wrote basically failed...
I cannot understand the functions I checked online, and an error occurred when I changed them ....

Under the guidance of various gods of csdn, I opened a slight link to the original question post...

Now we have released various functions for comrades who are already dead and not yet dead ....
I hope you can add some frequently-used methods.

Basic functions
-----------------------------------------------
In fact, mysql and php come with many functions that can handle character issues. Below are some frequently used functions.
Ps: Since php6 does not support magic_quotes_gpc at the beginning, the following things are assumed to be in the condition of magic_quotes_gpc = off (I don't know what new things will happen to php6 ....)

Mysql_real_escape_string ()
Definition: Special characters in strings used in function escape SQL statements.
Syntax: mysql_real_escape_string (string, connection)
Note: This function escapes special characters in string and considers the current character set to be connected. Therefore, it can be safely used for mysql_query ().
Because the instance code is too long, the function explanation link w3school phpnet is provided.

AddSlashes ()
Definition: The addslashes () function adds a backslash before a specified predefined character.
Syntax: addslashes (string)
Note: By default, the magic_quotes_gpc command of PHP is on, and addslashes () is automatically run for all GET, POST, and COOKIE data (). Do not use addslashes () for strings that have been escaped by magic_quotes_gpc, because this causes double-layer escape. In this case, you can use the get_magic_quotes_gpc () function for detection.
Because the instance code is too long, the function explanation link w3school phpnet is provided.
Related functions
Remove the backslash characters from StripSlashes. (Used to interpret characters to be filtered)

Mb_convert_encoding ()
PHP internal code conversion function
Version (PHP 4> = 4.0.6, PHP 5)
This function can convert various encodings to each other.
Related links: phpnet

Iconv ()
Php internal code conversion function, same as above
Because iconv () is a bug in gb2312 conversion, it should be handled like this
PHP code
  iconv( "UTF-8", "gb2312//IGNORE" , $str)

Ignore indicates that the conversion error is ignored. it is found that iconv fails to convert the character "-" to gb2312. without the ignore parameter, all strings after this character cannot be saved.
In addition, mb_convert_encoding does not have this bug, so the best syntax is:
PHP code
  mb_convert_encoding($str,"gb2312", "UTF-8");

However, enable mbstring Extension Library is required first.
You can also set the collation of the mysql database to UTF-8, which is not used for conversion.
Mysql statement
SQL code
  SET NAMES utf8;SET CHARACTER SET utf8;SET COLLATION_CONNECTION='utf8_general_ci';

Related links: phpnet
For more information, see link1 link2 link3.

Custom Function 1
-----------------------------------------------
The conversion function found on the Internet converts GB2312 to UTF-8, and the conversion error occurs after it is changed to UTF-8. Chinese characters cannot be parsed ...... we look forward to the regular expression mad man...
PHP code
  
  $v) {                 if(ord($v[0]) < 128)                            $ar[$k] = rawurlencode($v);                 else                            $ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v));                                                     }        return join("",$ar);}function unescape($str) {       $str = rawurldecode($str);       preg_match_all("/(?:%u.{4})|.+/",$str,$r);       $ar = $r[0];       foreach($ar as $k=>$v) {                if(substr($v,0,2) == "%u" && strlen($v) == 6)                $ar[$k] = iconv("UCS-2","GB2312",pack("H4",substr($v,-4)));                  }       return join("",$ar);}?>


Custom Function 2
-----------------------------------------------
Thanks to the gingzai777 Forum, the experts are different. you can see the problem at a glance .....
This line will be used for php filtering in the future, so you don't need to worry about file encoding .....
PHP code
  
  


-------------------------------------------------

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.