Php character escape function reference

Source: Internet
Author: User
Tags php regular expression
Php character escape function reference

  1. If (phpversion () <'5. 3.0 '){
  2. Set_magic_quotes_runtime (0 );
  3. }

> The magic_quotes_gpc cannot be defined through functions. Therefore, we recommend that you enable it on the server. when writing a program, you should judge it, avoid security problems caused by failing to enable gpc. when using addslashes to escape gpc, you should be aware of filtering key values and values when users submit array data.

  1. If (! Get_magic_quotes_gpc ()){

  2. $ _ Get = daddslashes ($ _ get );
  3. $ _ Post = daddslashes ($ _ post );
  4. $ _ Cookie = daddslashes ($ _ cookie );
  5. $ _ Files = daddslashes ($ _ files );
  6. }
  7. Function daddslashes ($ string, $ force = 1 ){
  8. If (is_array ($ string )){
  9. Foreach ($ string as $ key => $ val ){
  10. Unset ($ string [$ key]);
  11. $ String [addslashes ($ key)] = daddslashes ($ val, $ force );
  12. }
  13. } Else {
  14. $ String = addslashes ($ string );
  15. }
  16. Return $ string;
  17. }

  18. ?>

Escape html entities when users input or output to prevent xss vulnerability!

Today, I encountered a special character processing problem. I noticed this problem again in php:

  1. $ Str = "ffff \ 0 ffff ";
  2. Echo (strlen ($ str ));
  3. Echo ("\ n ");
  4. For ($ I = 0; $ I Echo ("\ n ");

Output result: ---------------------- 9 102 102 102 102 0 102 102 102

Example of replacing special characters

  1. $ Str = "ffff \ 0 ffff ";
  2. $ Str = str_replace ("\ x0", "", $ str );
  3. // Or use $ str = str_replace ("\ 0", "", $ str );
  4. // Or use $ str = str_replace (chr (0), "", $ str );
  5. Echo (strlen ($ str ));
  6. Echo ("\ n ");
  7. For ($ I = 0; $ I Echo ("\ n ");

Output result: ---------------------- 8 102 102 102 102 102 102 102

Octal ascii code example:

  1. // Note that the string that matches the regular \ [0-7] {} represents an octal ascii code.
  2. $ Str = "\ 0 \ 01 \ 02 \ 3 \ 7 \ 10 \ 011 \ 08 \ 8"; // The \ 8 here does not meet the requirements, corrected to "\ 8" (ascii: 92 and 56)
  3. Echo (strlen ($ str ));
  4. Echo ("\ n ");
  5. For ($ I = 0; $ I Echo ("\ n ");

Output result: ---------------------- 11 0 1 2 3 7 8 9 0 56 92 56

Hexadecimal ascii code example:

  1. $ Str = "\ x0 \ x1 \ x2 \ x3 \ x7 \ x8 \ x9 \ x10 \ x11 \ xff ";
  2. Echo (strlen ($ str ));
  3. Echo ("\ n ");
  4. For ($ I = 0; $ I Echo ("\ n ");

Output result: ---------------------- 10 0 1 2 3 7 8 9 16 17 255 php special character escape explanation php regular expression escape character example PHP common escape character function PHP escape regular expression character function php implement anti-injection code used to escape form submission values mysql statement character escape method example php character escape precautions

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.