Home  >  Article  >  Backend Development  >  PHP character escape function reference

PHP character escape function reference

WBOY
WBOYOriginal
2016-07-25 08:54:031269browse
  1. if(phpversion() < '5.3.0') {
  2. set_magic_quotes_runtime(0);
  3. }
Copy code

>> Magic_quotes_gpc cannot be defined through a function, so it is recommended to unify it on the server Turn it on. You should make a judgment when writing a program to avoid security issues caused by not turning on gpc. When escaping gpc through addslashes, attention should be paid to the filtering of key and value 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. ?>>

Copy code

Use to escape html entities when user input or output to prevent xss vulnerabilities produce!

Today I encountered a problem with processing special characters in files. I noticed this problem again. In php:

  1. $str = "ffff ----------------------- 9 102 102 102 102 0 102 102 102 102
  2. Example of replacing special characters
$str = "ffff ----------------------- 8 102 102 102 102 102 102 102 102

Octal ascii code example:

    //Note that a string that conforms to the regular pattern [0-7]{1,3} represents an octal ASCII code.
  1. $str = " ----------------------- 11 0 1 2 3 7 8 9 0 56 92 56
  2. Hexadecimal ascii code example:
  3. $str = "x0x1x2x3x7x8x9x10x11xff";
  4. echo(strlen($str));
echo("n");
for($i=0;$iecho("n");

Copy code

Output result: ----------------------- 10 0 1 2 3 7 8 9 16 17 255
Detailed explanation of php special character escaping
  1. Example of php regular expression escape characters
  2. Commonly used escape character functions in PHP
  3. PHP function to escape regular expression characters
  4. php code to implement anti-injection and form submission value escaping
Examples of character escaping methods in mysql statements
Notes on php character escaping

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