Annotation of embedaddon/php/ext/standard/tests/strings/strstr.phpt, revision 1.1.1.3

1.1       misho       1: --TEST--
                      2: Test strstr() function
                      3: --FILE--
                      4: <?php
                      5: /* Prototype: string strstr ( string $haystack, string $needle );
                      6:    Description: Find first occurrence of a string 
                      7:    and reurns the rest of the string from that string 
                      8: */
                      9: 
                     10: echo "*** Testing basic functionality of strstr() ***\n";
                     11: var_dump( strstr("test string", "test") );
                     12: var_dump( strstr("test string", "string") );
                     13: var_dump( strstr("test string", "strin") );
                     14: var_dump( strstr("test string", "t s") );
                     15: var_dump( strstr("test string", "g") );
                     16: var_dump( md5(strstr("te".chr(0)."st", chr(0))) );
                     17: var_dump( strstr("tEst", "test") );
                     18: var_dump( strstr("teSt", "test") );
                     19: var_dump( @strstr("", "") );
                     20: var_dump( @strstr("a", "") );
                     21: var_dump( @strstr("", "a") );
                     22: 
                     23: 
                     24: echo "\n*** Testing strstr() with various needles ***";
                     25: $string = 
                     26: "Hello world,012033 -3.3445     NULL TRUE FALSE\0 abcd\xxyz \x000 octal\n 
                     27: abcd$:Hello world";
                     28: 
                     29: /* needles in an array to get the string starts with needle, in $string */
                     30: $needles = array(
                     31:   "Hello world",       
                     32:   "WORLD", 
                     33:   "\0", 
                     34:   "\x00", 
                     35:   "\x000", 
                     36:   "abcd", 
                     37:   "xyz", 
                     38:   "octal", 
                     39:   "-3", 
                     40:   -3, 
                     41:   "-3.344", 
                     42:   -3.344, 
                     43:   NULL, 
                     44:   "NULL",
                     45:   "0",
                     46:   0, 
                     47:   TRUE, 
                     48:   "TRUE",
                     49:   "1",
                     50:   1,
                     51:   FALSE,
                     52:   "FALSE",
                     53:   " ",
                     54:   "     ",
                     55:   'b',
                     56:   '\n',
                     57:   "\n",
                     58:   "12",
                     59:   "12twelve",
                     60:   $string
                     61: );
                     62: 
                     63: /* loop through to get the string starts with "needle" in $string */
                     64: for( $i = 0; $i < count($needles); $i++ ) {
                     65:   echo "\n-- Iteration $i --\n";
                     66:   var_dump( strstr($string, $needles[$i]) );
                     67: }  
                     68: 
                     69:        
                     70: echo "\n*** Testing Miscelleneous input data ***\n";
                     71: 
                     72: echo "-- Passing objects as string and needle --\n";
                     73: /* we get "Catchable fatal error: saying Object of class needle could not be 
                     74: converted to string" by default when an object is passed instead of string:
1.1.1.3 ! misho      75: The error can be  avoided by choosing the __toString magix method as follows: */
1.1       misho      76: 
                     77: class string 
                     78: {
                     79:   function __toString() {
                     80:     return "Hello, world";
                     81:   }
                     82: }
                     83: $obj_string = new string;
                     84: 
                     85: class needle 
                     86: {
                     87:   function __toString() {
                     88:     return "world";
                     89:   }
                     90: }
                     91: $obj_needle = new needle;
                     92: 
                     93: var_dump(strstr("$obj_string", "$obj_needle"));        
                     94: 
                     95: 
                     96: echo "\n-- passing an array as string and needle --\n";
                     97: $needles = array("hello", "?world", "!$%**()%**[][[[&@#~!");
                     98: var_dump( strstr($needles, $needles) );  // won't work
                     99: var_dump( strstr("hello?world,!$%**()%**[][[[&@#~!", "$needles[1]") );  // works
                    100: var_dump( strstr("hello?world,!$%**()%**[][[[&@#~!", "$needles[2]") );  // works
                    101: 
                    102: 
                    103: echo "\n-- passing Resources as string and needle --\n"; 
                    104: $resource1 = fopen(__FILE__, "r");
                    105: $resource2 = opendir(".");
                    106: var_dump( strstr($resource1, $resource1) );
                    107: var_dump( strstr($resource1, $resource2) );
                    108: 
                    109: 
                    110: echo "\n-- Posiibilities with null --\n";
                    111: var_dump( strstr("", NULL) );
                    112: var_dump( strstr(NULL, NULL) );
                    113: var_dump( strstr("a", NULL) );
                    114: var_dump( strstr("/x0", "0") );  // Hexadecimal NUL
                    115: 
                    116: echo "\n-- A longer and heredoc string --\n";
                    117: $string = <<<EOD
                    118: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    119: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    120: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    121: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    122: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    123: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    124: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    125: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    126: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    127: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    128: EOD;
                    129: var_dump( strstr($string, "abcd") );
                    130: var_dump( strstr($string, "1234") );           
                    131: 
                    132: echo "\n-- A heredoc null string --\n";
                    133: $str = <<<EOD
                    134: EOD;
                    135: var_dump( strstr($str, "\0") );
                    136: var_dump( strstr($str, NULL) );
                    137: var_dump( strstr($str, "0") );
                    138: 
                    139: 
                    140: echo "\n-- simple and complex syntax strings --\n";
                    141: $needle = 'world';
                    142: 
                    143: /* Simple syntax */
                    144: var_dump( strstr("Hello, world", "$needle") );  // works 
                    145: var_dump( strstr("Hello, world'S", "$needle'S") );  // works
                    146: var_dump( strstr("Hello, worldS", "$needleS") );  // won't work 
                    147: 
                    148: /* String with curly braces, complex syntax */
                    149: var_dump( strstr("Hello, worldS", "${needle}S") );  // works
                    150: var_dump( strstr("Hello, worldS", "{$needle}S") );  // works
                    151: 
                    152: 
                    153: echo "\n-- complex strings containing other than 7-bit chars --\n";
                    154: $str = chr(0).chr(128).chr(129).chr(234).chr(235).chr(254).chr(255);
                    155: echo "- Positions of some chars in the string '$str' are as follows -\n";
                    156: echo chr(128)." => "; 
                    157: var_dump( strstr($str, chr(128)) );            
                    158: echo chr(255)." => "; 
                    159: var_dump( strstr($str, chr(255)) );
                    160: echo chr(256)." => "; 
                    161: var_dump( strstr($str, chr(256)) ); 
                    162: 
                    163: echo "\n*** Testing error conditions ***";
                    164: var_dump( strstr($string, ""));
                    165: var_dump( strstr() );  // zero argument
                    166: var_dump( strstr("") );  // null argument 
                    167: var_dump( strstr($string) );  // without "needle"
                    168: var_dump( strstr("a", "b", "c") );  // args > expected
                    169: var_dump( strstr(NULL, "") );
                    170: 
                    171: echo "\nDone";
                    172: 
                    173: fclose($resource1);
                    174: closedir($resource2);
                    175: ?>
                    176: --EXPECTF--
                    177: *** Testing basic functionality of strstr() ***
                    178: string(11) "test string"
                    179: string(6) "string"
                    180: string(6) "string"
                    181: string(8) "t string"
                    182: string(1) "g"
                    183: string(32) "7272696018bdeb2c9a3f8d01fc2a9273"
                    184: bool(false)
                    185: bool(false)
                    186: bool(false)
                    187: bool(false)
                    188: bool(false)
                    189: 
                    190: *** Testing strstr() with various needles ***
                    191: -- Iteration 0 --
                    192: string(86) "Hello world,012033 -3.3445     NULL TRUE FALSE abcd\xxyz 0 octal
                    193:  
                    194: abcd$:Hello world"
                    195: 
                    196: -- Iteration 1 --
                    197: bool(false)
                    198: 
                    199: -- Iteration 2 --
                    200: string(40) " abcd\xxyz 0 octal
                    201:  
                    202: abcd$:Hello world"
                    203: 
                    204: -- Iteration 3 --
                    205: string(40) " abcd\xxyz 0 octal
                    206:  
                    207: abcd$:Hello world"
                    208: 
                    209: -- Iteration 4 --
                    210: string(28) "0 octal
                    211:  
                    212: abcd$:Hello world"
                    213: 
                    214: -- Iteration 5 --
                    215: string(38) "abcd\xxyz 0 octal
                    216:  
                    217: abcd$:Hello world"
                    218: 
                    219: -- Iteration 6 --
                    220: string(32) "xyz 0 octal
                    221:  
                    222: abcd$:Hello world"
                    223: 
                    224: -- Iteration 7 --
                    225: string(25) "octal
                    226:  
                    227: abcd$:Hello world"
                    228: 
                    229: -- Iteration 8 --
                    230: string(67) "-3.3445     NULL TRUE FALSE abcd\xxyz 0 octal
                    231:  
                    232: abcd$:Hello world"
                    233: 
                    234: -- Iteration 9 --
                    235: bool(false)
                    236: 
                    237: -- Iteration 10 --
                    238: string(67) "-3.3445     NULL TRUE FALSE abcd\xxyz 0 octal
                    239:  
                    240: abcd$:Hello world"
                    241: 
                    242: -- Iteration 11 --
                    243: bool(false)
                    244: 
                    245: -- Iteration 12 --
                    246: string(40) " abcd\xxyz 0 octal
                    247:  
                    248: abcd$:Hello world"
                    249: 
                    250: -- Iteration 13 --
                    251: string(55) "NULL TRUE FALSE abcd\xxyz 0 octal
                    252:  
                    253: abcd$:Hello world"
                    254: 
                    255: -- Iteration 14 --
                    256: string(74) "012033 -3.3445     NULL TRUE FALSE abcd\xxyz 0 octal
                    257:  
                    258: abcd$:Hello world"
                    259: 
                    260: -- Iteration 15 --
                    261: string(40) " abcd\xxyz 0 octal
                    262:  
                    263: abcd$:Hello world"
                    264: 
                    265: -- Iteration 16 --
                    266: bool(false)
                    267: 
                    268: -- Iteration 17 --
                    269: string(50) "TRUE FALSE abcd\xxyz 0 octal
                    270:  
                    271: abcd$:Hello world"
                    272: 
                    273: -- Iteration 18 --
                    274: string(73) "12033 -3.3445     NULL TRUE FALSE abcd\xxyz 0 octal
                    275:  
                    276: abcd$:Hello world"
                    277: 
                    278: -- Iteration 19 --
                    279: bool(false)
                    280: 
                    281: -- Iteration 20 --
                    282: string(40) " abcd\xxyz 0 octal
                    283:  
                    284: abcd$:Hello world"
                    285: 
                    286: -- Iteration 21 --
                    287: string(45) "FALSE abcd\xxyz 0 octal
                    288:  
                    289: abcd$:Hello world"
                    290: 
                    291: -- Iteration 22 --
                    292: string(81) " world,012033 -3.3445     NULL TRUE FALSE abcd\xxyz 0 octal
                    293:  
                    294: abcd$:Hello world"
                    295: 
                    296: -- Iteration 23 --
                    297: string(60) "     NULL TRUE FALSE abcd\xxyz 0 octal
                    298:  
                    299: abcd$:Hello world"
                    300: 
                    301: -- Iteration 24 --
                    302: string(37) "bcd\xxyz 0 octal
                    303:  
                    304: abcd$:Hello world"
                    305: 
                    306: -- Iteration 25 --
                    307: bool(false)
                    308: 
                    309: -- Iteration 26 --
                    310: string(20) "
                    311:  
                    312: abcd$:Hello world"
                    313: 
                    314: -- Iteration 27 --
                    315: string(73) "12033 -3.3445     NULL TRUE FALSE abcd\xxyz 0 octal
                    316:  
                    317: abcd$:Hello world"
                    318: 
                    319: -- Iteration 28 --
                    320: bool(false)
                    321: 
                    322: -- Iteration 29 --
                    323: string(86) "Hello world,012033 -3.3445     NULL TRUE FALSE abcd\xxyz 0 octal
                    324:  
                    325: abcd$:Hello world"
                    326: 
                    327: *** Testing Miscelleneous input data ***
                    328: -- Passing objects as string and needle --
                    329: string(5) "world"
                    330: 
                    331: -- passing an array as string and needle --
                    332: 
                    333: Warning: strstr() expects parameter 1 to be string, array given in %s on line %d
                    334: NULL
                    335: string(27) "?world,!$%**()%**[][[[&@#~!"
                    336: string(20) "!$%**()%**[][[[&@#~!"
                    337: 
                    338: -- passing Resources as string and needle --
                    339: 
                    340: Warning: strstr() expects parameter 1 to be string, resource given in %s on line %d
                    341: NULL
                    342: 
                    343: Warning: strstr() expects parameter 1 to be string, resource given in %s on line %d
                    344: NULL
                    345: 
                    346: -- Posiibilities with null --
                    347: bool(false)
                    348: bool(false)
                    349: bool(false)
                    350: string(1) "0"
                    351: 
                    352: -- A longer and heredoc string --
                    353: string(729) "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    354: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    355: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    356: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    357: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    358: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    359: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    360: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    361: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    362: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                    363: string(702) "123456789abcdefghijklmnopqrstuvwxyz0123456789
                    364: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    365: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    366: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    367: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    368: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    369: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    370: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    371: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    372: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                    373: 
                    374: -- A heredoc null string --
                    375: bool(false)
                    376: bool(false)
                    377: bool(false)
                    378: 
                    379: -- simple and complex syntax strings --
                    380: string(5) "world"
                    381: string(7) "world'S"
                    382: 
                    383: Notice: Undefined variable: needleS in %s on line %d
                    384: 
1.1.1.2   misho     385: Warning: strstr(): Empty needle in %s on line %d
1.1       misho     386: bool(false)
                    387: string(6) "worldS"
                    388: string(6) "worldS"
                    389: 
                    390: -- complex strings containing other than 7-bit chars --
                    391: - Positions of some chars in the string '' are as follows -
                    392:  => string(6) ""
                    393:  => string(1) ""
                    394:  => string(7) ""
                    395: 
                    396: *** Testing error conditions ***
1.1.1.2   misho     397: Warning: strstr(): Empty needle in %s on line %d
1.1       misho     398: bool(false)
                    399: 
                    400: Warning: strstr() expects at least 2 parameters, 0 given in %s on line %d
                    401: NULL
                    402: 
                    403: Warning: strstr() expects at least 2 parameters, 1 given in %s on line %d
                    404: NULL
                    405: 
                    406: Warning: strstr() expects at least 2 parameters, 1 given in %s on line %d
                    407: NULL
                    408: bool(false)
                    409: 
1.1.1.2   misho     410: Warning: strstr(): Empty needle in %s on line %d
1.1       misho     411: bool(false)
                    412: 
                    413: Done

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>