1--TEST-- 2Test 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/* 11 * Test addslashes() with various strings containing characters thats can be backslashed 12*/ 13 14echo "*** Testing addslashes() : with various strings containing characters to be backslashed ***\n"; 15 16// initialising a heredoc string 17$heredoc_string = <<<EOT 18This is line 1 of 'heredoc' string 19This is line 2 of "heredoc" string 20EOT; 21 22$heredoc_null_string =<<<EOT 23EOT; 24 25// initialising the string array 26 27$str_array = array( 28 // string without any characters that can be backslashed 29 'Hello world', 30 31 // string with single quotes 32 "how're you doing?", 33 "don't disturb u'r neighbours", 34 "don't disturb u'r neighbours''", 35 '', 36 '\'', 37 "'", 38 39 // string with double quotes 40 'he said, "he will be on leave"', 41 'he said, ""he will be on leave"', 42 '"""PHP"""', 43 "", 44 "\"", 45 '"', 46 "hello\"", 47 48 // string with backslash characters 49 'Is your name Ram\Krishna?', 50 '\\0.0.0.0', 51 'c:\php\testcase\addslashes', 52 '\\', 53 54 // string with nul characters 55 'hello'.chr(0).'world', 56 chr(0).'hello'.chr(0), 57 chr(0).chr(0).'hello', 58 chr(0), 59 60 // mixed strings 61 "'\\0.0.0.0'", 62 "'\\0.0.0.0'".chr(0), 63 chr(0)."'c:\php\'", 64 '"\\0.0.0.0"', 65 '"c:\php\"'.chr(0)."'", 66 '"hello"'."'world'".chr(0).'//', 67 68 // string with hexadecimal number 69 "0xABCDEF0123456789", 70 "\x00", 71 '!@#$%&*@$%#&/;:,<>', 72 "hello\x00world", 73 74 // heredoc strings 75 $heredoc_string, 76 $heredoc_null_string 77 ); 78 79$count = 1; 80// looping to test for all strings in $str_array 81foreach( $str_array as $str ) { 82 echo "\n-- Iteration $count --\n"; 83 var_dump( addslashes($str) ); 84 $count ++; 85} 86 87echo "Done\n"; 88?> 89--EXPECTF-- 90*** Testing addslashes() : with various strings containing characters to be backslashed *** 91 92-- Iteration 1 -- 93string(11) "Hello world" 94 95-- Iteration 2 -- 96string(18) "how\'re you doing?" 97 98-- Iteration 3 -- 99string(30) "don\'t disturb u\'r neighbours" 100 101-- Iteration 4 -- 102string(34) "don\'t disturb u\'r neighbours\'\'" 103 104-- Iteration 5 -- 105string(0) "" 106 107-- Iteration 6 -- 108string(2) "\'" 109 110-- Iteration 7 -- 111string(2) "\'" 112 113-- Iteration 8 -- 114string(32) "he said, \"he will be on leave\"" 115 116-- Iteration 9 -- 117string(34) "he said, \"\"he will be on leave\"" 118 119-- Iteration 10 -- 120string(15) "\"\"\"PHP\"\"\"" 121 122-- Iteration 11 -- 123string(0) "" 124 125-- Iteration 12 -- 126string(2) "\"" 127 128-- Iteration 13 -- 129string(2) "\"" 130 131-- Iteration 14 -- 132string(7) "hello\"" 133 134-- Iteration 15 -- 135string(26) "Is your name Ram\\Krishna?" 136 137-- Iteration 16 -- 138string(9) "\\0.0.0.0" 139 140-- Iteration 17 -- 141string(29) "c:\\php\\testcase\\addslashes" 142 143-- Iteration 18 -- 144string(2) "\\" 145 146-- Iteration 19 -- 147string(12) "hello\0world" 148 149-- Iteration 20 -- 150string(9) "\0hello\0" 151 152-- Iteration 21 -- 153string(9) "\0\0hello" 154 155-- Iteration 22 -- 156string(2) "\0" 157 158-- Iteration 23 -- 159string(13) "\'\\0.0.0.0\'" 160 161-- Iteration 24 -- 162string(15) "\'\\0.0.0.0\'\0" 163 164-- Iteration 25 -- 165string(15) "\0\'c:\\php\\\'" 166 167-- Iteration 26 -- 168string(13) "\"\\0.0.0.0\"" 169 170-- Iteration 27 -- 171string(17) "\"c:\\php\\\"\0\'" 172 173-- Iteration 28 -- 174string(22) "\"hello\"\'world\'\0//" 175 176-- Iteration 29 -- 177string(18) "0xABCDEF0123456789" 178 179-- Iteration 30 -- 180string(2) "\0" 181 182-- Iteration 31 -- 183string(18) "!@#$%&*@$%#&/;:,<>" 184 185-- Iteration 32 -- 186string(12) "hello\0world" 187 188-- Iteration 33 -- 189string(73) "This is line 1 of \'heredoc\' string 190This is line 2 of \"heredoc\" string" 191 192-- Iteration 34 -- 193string(0) "" 194Done 195