PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/standard/tests/strings/addslashes_variation2.phpt

http://github.com/infusion/PHP
Unknown | 194 lines | 145 code | 49 blank | 0 comment | 0 complexity | 904d6f5289fb2b99ca8309787b2243f3 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. --TEST--
  2. Test addslashes() function : usage variations - strings with characters to be backslashed
  3. --FILE--
  4. <?php
  5. /* Prototype : string addslashes ( string $str )
  6. * Description: Returns a string with backslashes before characters that need to be quoted in database queries etc.
  7. * Source code: ext/standard/string.c
  8. */
  9. /*
  10. * Test addslashes() with various strings containing characters thats can be backslashed
  11. */
  12. echo "*** Testing addslashes() : with various strings containing characters to be backslashed ***\n";
  13. // initialising a heredoc string
  14. $heredoc_string = <<<EOT
  15. This is line 1 of 'heredoc' string
  16. This is line 2 of "heredoc" string
  17. EOT;
  18. $heredoc_null_string =<<<EOT
  19. EOT;
  20. // initialising the string array
  21. $str_array = array(
  22. // string without any characters that can be backslashed
  23. 'Hello world',
  24. // string with single quotes
  25. "how're you doing?",
  26. "don't disturb u'r neighbours",
  27. "don't disturb u'r neighbours''",
  28. '',
  29. '\'',
  30. "'",
  31. // string with double quotes
  32. 'he said, "he will be on leave"',
  33. 'he said, ""he will be on leave"',
  34. '"""PHP"""',
  35. "",
  36. "\"",
  37. '"',
  38. "hello\"",
  39. // string with backslash characters
  40. 'Is your name Ram\Krishna?',
  41. '\\0.0.0.0',
  42. 'c:\php\testcase\addslashes',
  43. '\\',
  44. // string with nul characters
  45. 'hello'.chr(0).'world',
  46. chr(0).'hello'.chr(0),
  47. chr(0).chr(0).'hello',
  48. chr(0),
  49. // mixed strings
  50. "'\\0.0.0.0'",
  51. "'\\0.0.0.0'".chr(0),
  52. chr(0)."'c:\php\'",
  53. '"\\0.0.0.0"',
  54. '"c:\php\"'.chr(0)."'",
  55. '"hello"'."'world'".chr(0).'//',
  56. // string with hexadecimal number
  57. "0xABCDEF0123456789",
  58. "\x00",
  59. '!@#$%&*@$%#&/;:,<>',
  60. "hello\x00world",
  61. // heredoc strings
  62. $heredoc_string,
  63. $heredoc_null_string
  64. );
  65. $count = 1;
  66. // looping to test for all strings in $str_array
  67. foreach( $str_array as $str ) {
  68. echo "\n-- Iteration $count --\n";
  69. var_dump( addslashes($str) );
  70. $count ++;
  71. }
  72. echo "Done\n";
  73. ?>
  74. --EXPECTF--
  75. *** Testing addslashes() : with various strings containing characters to be backslashed ***
  76. -- Iteration 1 --
  77. string(11) "Hello world"
  78. -- Iteration 2 --
  79. string(18) "how\'re you doing?"
  80. -- Iteration 3 --
  81. string(30) "don\'t disturb u\'r neighbours"
  82. -- Iteration 4 --
  83. string(34) "don\'t disturb u\'r neighbours\'\'"
  84. -- Iteration 5 --
  85. string(0) ""
  86. -- Iteration 6 --
  87. string(2) "\'"
  88. -- Iteration 7 --
  89. string(2) "\'"
  90. -- Iteration 8 --
  91. string(32) "he said, \"he will be on leave\""
  92. -- Iteration 9 --
  93. string(34) "he said, \"\"he will be on leave\""
  94. -- Iteration 10 --
  95. string(15) "\"\"\"PHP\"\"\""
  96. -- Iteration 11 --
  97. string(0) ""
  98. -- Iteration 12 --
  99. string(2) "\""
  100. -- Iteration 13 --
  101. string(2) "\""
  102. -- Iteration 14 --
  103. string(7) "hello\""
  104. -- Iteration 15 --
  105. string(26) "Is your name Ram\\Krishna?"
  106. -- Iteration 16 --
  107. string(9) "\\0.0.0.0"
  108. -- Iteration 17 --
  109. string(29) "c:\\php\\testcase\\addslashes"
  110. -- Iteration 18 --
  111. string(2) "\\"
  112. -- Iteration 19 --
  113. string(12) "hello\0world"
  114. -- Iteration 20 --
  115. string(9) "\0hello\0"
  116. -- Iteration 21 --
  117. string(9) "\0\0hello"
  118. -- Iteration 22 --
  119. string(2) "\0"
  120. -- Iteration 23 --
  121. string(13) "\'\\0.0.0.0\'"
  122. -- Iteration 24 --
  123. string(15) "\'\\0.0.0.0\'\0"
  124. -- Iteration 25 --
  125. string(15) "\0\'c:\\php\\\'"
  126. -- Iteration 26 --
  127. string(13) "\"\\0.0.0.0\""
  128. -- Iteration 27 --
  129. string(17) "\"c:\\php\\\"\0\'"
  130. -- Iteration 28 --
  131. string(22) "\"hello\"\'world\'\0//"
  132. -- Iteration 29 --
  133. string(18) "0xABCDEF0123456789"
  134. -- Iteration 30 --
  135. string(2) "\0"
  136. -- Iteration 31 --
  137. string(18) "!@#$%&*@$%#&/;:,<>"
  138. -- Iteration 32 --
  139. string(12) "hello\0world"
  140. -- Iteration 33 --
  141. string(73) "This is line 1 of \'heredoc\' string
  142. This is line 2 of \"heredoc\" string"
  143. -- Iteration 34 --
  144. string(0) ""
  145. Done