cvs: php-src /ext/standard/tests/strings addcslashes.phpt chr_ord.phpt implode1.phpt md5_file.phpt str_repeat.phpt str_replace.phpt strpos.phpt
strstr.phpt substr.phpt substr_count.phpt trim1.phpt
| From: | Raghubansh Kumar | Date: | Sat, 12 May 2007 12:12:26 +0000 |
| Subject: | cvs: php-src /ext/standard/tests/strings addcslashes.phpt chr_ord.phpt implode1.phpt md5_file.phpt str_repeat.phpt str_replace.phpt strpos.phpt strstr.phpt substr.phpt substr_count.phpt trim1.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-44277@lists.php.net to get a copy of this message | ||
kraghuba Sat May 12 12:12:26 2007 UTC
Modified files:
/php-src/ext/standard/tests/strings addcslashes.phpt md5_file.phpt
trim1.phpt substr.phpt
substr_count.phpt implode1.phpt
str_repeat.phpt strpos.phpt
chr_ord.phpt str_replace.phpt
strstr.phpt
Log:
New version of addcslashes.phpt, implode1.phpt, strpos.phpt, str_replace.phpt, substr_count.phpt,
trim1.phpt, chr_ord.phpt, md5_file.phpt, str_repeat.phpt, strstr.phpt, substr.phpt
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/addcslashes.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/strings/addcslashes.phpt http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/md5_file.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/strings/md5_file.phpt http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/trim1.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/strings/trim1.phpt http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/substr.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/strings/substr.phpt diff -u /dev/null php-src/ext/standard/tests/strings/substr.phpt:1.2 --- /dev/null Sat May 12 12:12:26 2007 +++ php-src/ext/standard/tests/strings/substr.phpt Sat May 12 12:12:26 2007 @@ -0,0 +1,218 @@ +--TEST-- +Testing substr() function +--FILE-- +<?php + +/* Prototype: string substr( string str, int start[, int length] ) + * Description: Returns the portion of string specified by the start and length parameters. + */ + +$strings_array = array( NULL, "", 12345, "abcdef", "123abc", "_123abc"); + + +/* Testing for error conditions */ +echo "*** Testing for error conditions ***\n"; + +/* Zero Argument */ +var_dump( substr() ); + +/* NULL as Argument */ +var_dump( substr(NULL) ); + +/* Single Argument */ +var_dump( substr("abcde") ); + +/* Scalar Argument */ +var_dump( substr(12345) ); + +/* more than valid number of arguments ( valid are 2 or 3 ) */ +var_dump( substr("abcde", 2, 3, 4) ); + +$counter = 1; +foreach ($strings_array as $str) { + /* variations with two arguments */ + /* start values >, < and = 0 */ + + echo ("\n--- Iteration ".$counter." ---\n"); + echo ("\n-- Variations for two arguments --\n"); + var_dump ( substr($str, 1) ); + var_dump ( substr($str, 0) ); + var_dump ( substr($str, -2) ); + + /* variations with three arguments */ + /* start value variations with length values */ + + echo ("\n-- Variations for three arguments --\n"); + var_dump ( substr($str, 1, 3) ); + var_dump ( substr($str, 1, 0) ); + var_dump ( substr($str, 1, -3) ); + var_dump ( substr($str, 0, 3) ); + var_dump ( substr($str, 0, 0) ); + var_dump ( substr($str, 0, -3) ); + var_dump ( substr($str, -2, 3) ); + var_dump ( substr($str, -2, 0 ) ); + var_dump ( substr($str, -2, -3) ); + + $counter++; +} + +/* variation of start and length to point to same element */ +echo ("\n*** Testing for variations of start and length to point to same element ***\n"); +var_dump (substr("abcde" , 2, -2) ); +var_dump (substr("abcde" , -3, -2) ); + +/* Testing to return empty string when start denotes the position beyond the truncation (set by negative length) */ +echo ("\n*** Testing for start > truncation ***\n"); +var_dump (substr("abcdef" , 4, -4) ); + +/* String with null character */ +echo ("\n*** Testing for string with null characters ***\n"); +var_dump (substr("abc\x0xy\x0z" ,2) ); + +/* String with international characters */ +echo ("\n*** Testing for string with international characters ***\n"); +var_dump (substr('\xIñtërnâtiônà lizætiøn',3) ); + +echo"\nDone"; + +?> +--EXPECTF-- +*** Testing for error conditions *** + +Warning: substr() expects at least 2 parameters, 0 given in %s on line %d +NULL + +Warning: substr() expects at least 2 parameters, 1 given in %s on line %d +NULL + +Warning: substr() expects at least 2 parameters, 1 given in %s on line %d +NULL + +Warning: substr() expects at least 2 parameters, 1 given in %s on line %d +NULL + +Warning: substr() expects at most 3 parameters, 4 given in %s on line %d +NULL + +--- Iteration 1 --- + +-- Variations for two arguments -- +bool(false) +bool(false) +bool(false) + +-- Variations for three arguments -- +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) + +--- Iteration 2 --- + +-- Variations for two arguments -- +bool(false) +bool(false) +bool(false) + +-- Variations for three arguments -- +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) + +--- Iteration 3 --- + +-- Variations for two arguments -- +string(4) "2345" +string(5) "12345" +string(2) "45" + +-- Variations for three arguments -- +string(3) "234" +string(0) "" +string(1) "2" +string(3) "123" +string(0) "" +string(2) "12" +string(2) "45" +string(0) "" +string(0) "" + +--- Iteration 4 --- + +-- Variations for two arguments -- +string(5) "bcdef" +string(6) "abcdef" +string(2) "ef" + +-- Variations for three arguments -- +string(3) "bcd" +string(0) "" +string(2) "bc" +string(3) "abc" +string(0) "" +string(3) "abc" +string(2) "ef" +string(0) "" +string(0) "" + +--- Iteration 5 --- + +-- Variations for two arguments -- +string(5) "23abc" +string(6) "123abc" +string(2) "bc" + +-- Variations for three arguments -- +string(3) "23a" +string(0) "" +string(2) "23" +string(3) "123" +string(0) "" +string(3) "123" +string(2) "bc" +string(0) "" +string(0) "" + +--- Iteration 6 --- + +-- Variations for two arguments -- +string(6) "123abc" +string(7) "_123abc" +string(2) "bc" + +-- Variations for three arguments -- +string(3) "123" +string(0) "" +string(3) "123" +string(3) "_12" +string(0) "" +string(4) "_123" +string(2) "bc" +string(0) "" +string(0) "" + +*** Testing for variations of start and length to point to same element *** +string(1) "c" +string(1) "c" + +*** Testing for start > truncation *** +string(0) "" + +*** Testing for string with null characters *** +string(6) "c+ +*** Testing for string with international characters *** +string(26) "ñtërnâtiônà lizætiøn" + +Done http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/substr_count.phpt?r1=1.3&r2=1.4&diff_format=u Index: php-src/ext/standard/tests/strings/substr_count.phpt diff -u php-src/ext/standard/tests/strings/substr_count.phpt:1.3 php-src/ext/standard/tests/strings/substr_count.phpt:1.4 --- php-src/ext/standard/tests/strings/substr_count.phpt:1.3 Sat Jun 18 18:23:12 2005 +++ php-src/ext/standard/tests/strings/substr_count.phpt Sat May 12 12:12:26 2007 @@ -1,23 +1,99 @@ --TEST-- -substr_count() function +Test substr_count() function --FILE-- <?php - var_dump(@substr_count("", "")); - var_dump(@substr_count("a", "")); - var_dump(@substr_count("", "a")); - var_dump(@substr_count("", "a")); - var_dump(@substr_count("", chr(0))); +/* Prototype: int substr_count ( string $haystack, string $needle [, int $offset [, int $length]] ); + * Description: substr_count() returns the number of times the needle substring occurs in the + * haystack string. Please note that needle is case sensitive + */ + +/* Count the number of substring occurrences */ +echo "***Testing basic operations ***\n"; +var_dump(@substr_count("", "")); +var_dump(@substr_count("a", "")); +var_dump(@substr_count("", "a")); +var_dump(@substr_count("", "a")); +var_dump(@substr_count("", chr(0))); +$a = str_repeat("abcacba", 100); +var_dump(@substr_count($a, "bca")); +$a = str_repeat("abcacbabca", 100); +var_dump(@substr_count($a, "bca")); +var_dump(substr_count($a, "bca", 200)); +var_dump(substr_count($a, "bca", 200, 50)); + +echo "\n*** Testing possible variations ***\n"; +echo "-- 3rd or 4th arg as string --\n"; +$str = "this is a string"; +var_dump( substr_count($str, "t", "5") ); +var_dump( substr_count($str, "t", "5", "10") ); +var_dump( substr_count($str, "i", "5t") ); +var_dump( substr_count($str, "i", "5t", "10t") ); + +echo "\n-- 3rd or 4th arg as NULL --\n"; +var_dump( substr_count($str, "t", "") ); +var_dump( substr_count($str, "T", "") ); +var_dump( substr_count($str, "t", "", 15) ); +var_dump( substr_count($str, "I", NULL) ); +var_dump( substr_count($str, "i", NULL, 10) ); + +echo "\n-- overlapped substrings --\n"; +var_dump( substr_count("abcabcabcabcabc", "abca") ); +var_dump( substr_count("abcabcabcabcabc", "abca", 2) ); + +echo "\n-- complex strings containing other than 7-bit chars --\n"; +$str = chr(128).chr(129).chr(128).chr(256).chr(255).chr(254).chr(255); +var_dump(substr_count($str, chr(128))); +var_dump(substr_count($str, chr(255))); +var_dump(substr_count($str, chr(256))); + +echo "\n-- heredoc string --\n"; +$string = <<<EOD +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +acdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +acdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +EOD; +var_dump(substr_count($string, "abcd")); +var_dump(substr_count($string, "1234")); + +echo "\n-- heredoc null string --\n"; +$str = <<<EOD +EOD; +var_dump(substr_count($str, "\0")); +var_dump(substr_count($str, "\x000")); +var_dump(substr_count($str, "0")); + + +echo "\n*** Testing error conditions ***\n"; +/* Zero argument */ +var_dump( substr_count() ); + +/* more than expected no. of args */ +var_dump( substr_count($str, "t", 0, 15, 30) ); - $a = str_repeat("abcacba", 100); - var_dump(@substr_count($a, "bca")); +/* offset as negative value */ +var_dump(substr_count($str, "t", -5)); + +/* offset > size of the string */ +var_dump(substr_count($str, "t", 25)); + +/* Using offset and length to go beyond the size of the string: + Warning message expected, as length+offset > length of string */ +var_dump( substr_count($str, "i", 5, 15) ); + +/* length as Null */ +var_dump( substr_count($str, "t", "", "") ); +var_dump( substr_count($str, "i", NULL, NULL) ); - $a = str_repeat("abcacbabca", 100); - var_dump(@substr_count($a, "bca")); +echo "Done\n"; - var_dump(substr_count($a, "bca", 200)); - var_dump(substr_count($a, "bca", 200, 50)); ?> ---EXPECT-- +--EXPECTF-- +***Testing basic operations *** bool(false) bool(false) int(0) @@ -27,3 +103,69 @@ int(200) int(160) int(10) + +*** Testing possible variations *** +-- 3rd or 4th arg as string -- +int(1) +int(1) + +Notice: A non well formed numeric value encountered in %s on line %d +int(2) + +Notice: A non well formed numeric value encountered in %s on line %d + +Notice: A non well formed numeric value encountered in %s on line %d +int(2) + +-- 3rd or 4th arg as NULL -- + +Warning: substr_count() expects parameter 3 to be long, string given in %s on line %d +NULL + +Warning: substr_count() expects parameter 3 to be long, string given in %s on line %d +NULL + +Warning: substr_count() expects parameter 3 to be long, string given in %s on line %d +NULL +int(0) +int(2) + +-- overlapped substrings -- +int(2) +int(2) + +-- complex strings containing other than 7-bit chars -- +int(2) +int(2) +int(1) + +-- heredoc string -- +int(14) +int(16) + +-- heredoc null string -- +int(0) +int(0) +int(0) + +*** Testing error conditions *** + +Warning: substr_count() expects at least 2 parameters, 0 given in %s on line %d +NULL + +Warning: substr_count() expects at most 4 parameters, 5 given in %s on line %d +NULL + +Warning: substr_count(): Offset should be greater than or equal to 0 in %s on line %d +bool(false) + +Warning: substr_count(): Offset value 25 exceeds string length in %s on line %d +bool(false) + +Warning: substr_count(): Offset value 5 exceeds string length in %s on line %d +bool(false) + +Warning: substr_count() expects parameter 3 to be long, string given in %s on line %d +NULL +int(0) +Done http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/implode1.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/strings/implode1.phpt http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/str_repeat.phpt?r1=1.2&r2=1.3&diff_format=u Index: php-src/ext/standard/tests/strings/str_repeat.phpt http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/strpos.phpt?r1=1.2&r2=1.3&diff_format=u Index: php-src/ext/standard/tests/strings/strpos.phpt diff -u php-src/ext/standard/tests/strings/strpos.phpt:1.2 php-src/ext/standard/tests/strings/strpos.phpt:1.3 --- php-src/ext/standard/tests/strings/strpos.phpt:1.2 Wed May 19 08:45:23 2004 +++ php-src/ext/standard/tests/strings/strpos.phpt Sat May 12 12:12:26 2007 @@ -1,21 +1,199 @@ --TEST-- -strpos() function +Test strpos() function --FILE-- <?php - var_dump(strpos("test string", "test")); - var_dump(strpos("test string", "string")); - var_dump(strpos("test string", "strin")); - var_dump(strpos("test string", "t s")); - var_dump(strpos("test string", "g")); - var_dump(strpos("te".chr(0)."st", chr(0))); - var_dump(strpos("tEst", "test")); - var_dump(strpos("teSt", "test")); - var_dump(@strpos("", "")); - var_dump(@strpos("a", "")); - var_dump(@strpos("", "a")); - var_dump(@strpos("\\\\a", "\\a")); +/* Prototype: int strpos ( string $haystack, mixed $needle [, int $offset] ); + Description: Find position of first occurrence of a string +*/ + +echo "*** Testing basic functionality of strpos() ***\n"; +var_dump( strpos("test string", "test") ); +var_dump( strpos("test string", "string") ); +var_dump( strpos("test string", "strin") ); +var_dump( strpos("test string", "t s") ); +var_dump( strpos("test string", "g") ); +var_dump( strpos("te".chr(0)."st", chr(0)) ); +var_dump( strpos("tEst", "test") ); +var_dump( strpos("teSt", "test") ); +var_dump( @strpos("", "") ); +var_dump( @strpos("a", "") ); +var_dump( @strpos("", "a") ); +var_dump( @strpos("\\\\a", "\\a") ); + + +echo "\n*** Testing stropos() to find various needles and a long string ***\n"; +$string = +"Hello world,012033 -3.3445 NULL TRUE FALSE\0 abcd\xxyz \x000 octal\n +abcd$:Hello world"; + +/* needles in an array to get the position of needle in $string */ +$needles = array( + "Hello world", + "WORLD", + "\0", + "\x00", + "\x000", + "abcd", + "xyz", + "octal", + "-3", + -3, + "-3.344", + -3.344, + NULL, + "NULL", + "0", + 0, + TRUE, + "TRUE", + "1", + 1, + FALSE, + "FALSE", + " ", + " ", + 'b', + '\n', + "\n", + "12", + "12twelve", + $string +); + +/* loop through to get the "needle" position in $string */ +for( $i = 0; $i < count($needles); $i++ ) { + echo "Position of '$needles[$i]' is => "; + var_dump( strpos($string, $needles[$i]) ); +} + + +echo "\n*** Testing strpos() with possible variations in offset ***\n"; +$offset_values = array ( + 1, // offset = 1 + NULL, // offset as string, converts to zero + TRUE, + NULL, + FALSE, +); + +/* loop through to get the "needle" position in $string */ +for( $i = 0; $i < count( $offset_values ); $i++ ) { + echo "Position of 'Hello' with offset '$offset_values[$i]' is => "; + var_dump( strpos($string, "Hello", $offset_values[$i]) ); +} + + +echo "\n*** Testing Miscelleneous input data ***\n"; + +echo "-- Passing objects as string and needle --\n"; +/* we get "Catchable fatal error: saying Object of class needle could not be + converted to string" by default when an object is passed instead of string: + The error can be avoided by chosing the __toString magix method as follows: */ + +class string +{ + function __toString() { + return "Hello, world"; + } +} +$obj_string = new string; + +class needle +{ + function __toString() { + return "world"; + } +} +$obj_needle = new needle; + +var_dump( strpos("$obj_string", "$obj_needle") ); + +echo "\n-- Passing an array as string and needle --\n"; +$needles = array("hello", "?world", "!$%**()%**[][[[&@#~!"); +var_dump( strpos($needles, $needles) ); // won't work +var_dump( strpos("hello?world,!$%**()%**[][[[&@#~!", "$needles[1]") ); // works +var_dump( strpos("hello?world,!$%**()%**[][[[&@#~!", "$needles[2]") ); // works + + +echo "\n-- Passing Resources as string and needle --\n"; +$resource1 = fopen(__FILE__, "r"); +$resource2 = opendir("."); +var_dump( strpos($resource1, $resource1) ); +var_dump( strpos($resource1, $resource2) ); + +echo "\n-- Posiibilities with null --\n"; +var_dump( strpos("", NULL) ); +var_dump( strpos(NULL, NULL) ); +var_dump( strpos("a", NULL) ); +var_dump( strpos("/x0", "0") ); // Hexadecimal NUL + +echo "\n-- A longer and heredoc string --\n"; +$string = <<<EOD +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +EOD; +var_dump( strpos($string, "abcd") ); +var_dump( strpos($string, "abcd", 72) ); // 72 -> "\n" in the first line +var_dump( strpos($string, "abcd", 73) ); // 73 -> "abcd" in the second line +var_dump( strpos($string, "9", (strlen($string)-1)) ); + +echo "\n-- A heredoc null string --\n"; +$str = <<<EOD +EOD; +var_dump( strpos($str, "\0") ); +var_dump( strpos($str, NULL) ); +var_dump( strpos($str, "0") ); + + +echo "\n-- simple and complex syntax strings --\n"; +$needle = 'world'; + +/* Simple syntax */ +var_dump( strpos("Hello, world", "$needle") ); // works +var_dump( strpos("Hello, world'S", "$needle'S") ); // works +var_dump( strpos("Hello, worldS", "$needleS") ); // won't work + +/* String with curly braces, complex syntax */ +var_dump( strpos("Hello, worldS", "${needle}S") ); // works +var_dump( strpos("Hello, worldS", "{$needle}S") ); // works + + +echo "\n-- complex strings containing other than 7-bit chars --\n"; +$str = chr(0).chr(128).chr(129).chr(234).chr(235).chr(254).chr(255); +echo "-- Positions of some chars in the string '$str' are as follows --\n"; +echo chr(128)." => "; +var_dump( strpos($str, chr(128)) ); +echo chr(255)." => "; +var_dump( strpos($str, chr(255), 3) ); +echo chr(256)." => "; +var_dump( strpos($str, chr(256)) ); + +echo "\n*** Testing error conditions ***"; +var_dump( strpos($string, "") ); +var_dump( strpos() ); // zero argument +var_dump( strpos("") ); // null argument +var_dump( strpos($string) ); // without "needle" +var_dump( strpos("a", "b", "c") ); // string as offset +var_dump( strpos("a", "b", "c", "d") ); // args > expected +var_dump( strpos($string, "test", strlen($string)+1) ); // offset > strlen() +var_dump( strpos($string, "test", -1) ); // offset < 0 +var_dump( strpos(NULL, "") ); + +fclose($resource1); +closedir($resource2); + +echo "\nDone"; ?> ---EXPECT-- +--EXPECTF-- +*** Testing basic functionality of strpos() *** int(0) int(5) int(5) @@ -28,3 +206,128 @@ bool(false) bool(false) int(1) + +*** Testing stropos() to find various needles and a long string *** +Position of 'Hello world' is => int(0) +Position of 'WORLD' is => bool(false) +Position of '+Position of '+Position of '+Position of 'abcd' is => int(48) +Position of 'xyz' is => int(54) +Position of 'octal' is => int(61) +Position of '-3' is => int(19) +Position of '-3' is => bool(false) +Position of '-3.344' is => int(19) +Position of '-3.344' is => bool(false) +Position of '' is => int(46) +Position of 'NULL' is => int(31) +Position of '0' is => int(12) +Position of '0' is => int(46) +Position of '1' is => bool(false) +Position of 'TRUE' is => int(36) +Position of '1' is => int(13) +Position of '1' is => bool(false) +Position of '' is => int(46) +Position of 'FALSE' is => int(41) +Position of ' ' is => int(5) +Position of ' ' is => int(26) +Position of 'b' is => int(49) +Position of '\n' is => bool(false) +Position of ' +' is => int(66) +Position of '12' is => int(13) +Position of '12twelve' is => bool(false) +Position of 'Hello world,012033 -3.3445 NULL TRUE FALSE+ +abcd$:Hello world' is => int(0) + +*** Testing strpos() with possible variations in offset *** +Position of 'Hello' with offset '1' is => int(74) +Position of 'Hello' with offset '' is => int(0) +Position of 'Hello' with offset '1' is => int(74) +Position of 'Hello' with offset '' is => int(0) +Position of 'Hello' with offset '' is => int(0) + +*** Testing Miscelleneous input data *** +-- Passing objects as string and needle -- +int(7) + +-- Passing an array as string and needle -- + +Warning: strpos() expects parameter 1 to be string (Unicode or binary), array given in %s on line 110 +NULL +int(5) +int(12) + +-- Passing Resources as string and needle -- + +Warning: strpos() expects parameter 1 to be string (Unicode or binary), resource given in %s on line 118 +NULL + +Warning: strpos() expects parameter 1 to be string (Unicode or binary), resource given in %s on line 119 +NULL + +-- Posiibilities with null -- +bool(false) +bool(false) +bool(false) +int(2) + +-- A longer and heredoc string -- +int(0) +int(73) +int(73) +int(728) + +-- A heredoc null string -- +bool(false) +bool(false) +bool(false) + +-- simple and complex syntax strings -- +int(7) +int(7) + +Notice: Undefined variable: needleS in %s on line 159 + +Warning: strpos(): Empty delimiter in %s on line 159 +bool(false) +int(7) +int(7) + +-- complex strings containing other than 7-bit chars -- +-- Positions of some chars in the string '+ => int(1) +ÿ => int(6) ++ +*** Testing error conditions *** +Warning: strpos(): Empty delimiter in %s on line %d +bool(false) + +Warning: strpos() expects at least 2 parameters, 0 given in %s on line %d +NULL + +Warning: strpos() expects at least 2 parameters, 1 given in %s on line %d +NULL + +Warning: strpos() expects at least 2 parameters, 1 given in %s on line %d +NULL + +Warning: strpos() expects parameter 3 to be long, string given in %s on line %d +NULL + +Warning: strpos() expects at most 3 parameters, 4 given in %s on line %d +NULL + +Warning: strpos(): Offset not contained in string in %s on line %d +bool(false) + +Warning: strpos(): Offset not contained in string in %s on line %d +bool(false) + +Warning: strpos(): Empty delimiter in %s on line %d +bool(false) + +Done http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/chr_ord.phpt?r1=1.2&r2=1.3&diff_format=u Index: php-src/ext/standard/tests/strings/chr_ord.phpt http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/str_replace.phpt?r1=1.2&r2=1.3&diff_format=u Index: php-src/ext/standard/tests/strings/str_replace.phpt diff -u php-src/ext/standard/tests/strings/str_replace.phpt:1.2 php-src/ext/standard/tests/strings/str_replace.phpt:1.3 --- php-src/ext/standard/tests/strings/str_replace.phpt:1.2 Thu Nov 9 01:16:48 2006 +++ php-src/ext/standard/tests/strings/str_replace.phpt Sat May 12 12:12:26 2007 @@ -1,40 +1,168 @@ --TEST-- -str_replace() tests +Test str_replace() function --FILE-- <?php +/* + Prototype: mixed str_replace(mixed $search, mixed $replace, + mixed $subject [, int &$count]); + Description: Replace all occurrences of the search string with + the replacement string +*/ + +echo "\n*** Testing str_replace() on basic operations ***\n"; + +var_dump( str_replace("", "", "") ); + +var_dump( str_replace("e", "b", "test") ); + +var_dump( str_replace("", "", "", $count) ); +var_dump( $count ); + +var_dump( str_replace("q", "q", "q", $count) ); +var_dump( $count ); + +var_dump( str_replace("long string here", "", "", $count) ); +var_dump( $count ); + +$fp = fopen( __FILE__, "r" ); +var_dump( str_replace($fp, $fp, $fp, $fp) ); +var_dump( $fp ); + +echo "\n*** Testing str_replace() with various search values ***"; +$search_arr = array( TRUE, FALSE, 1, 0, -1, "1", "0", "-1", NULL, + array(), "php", ""); + +$i = 0; +/* loop through to replace the matched elements in the array */ +foreach( $search_arr as $value ) { + echo "\n-- Iteration $i --\n"; + /* replace the string in array */ + var_dump( str_replace($value, "FOUND", $search_arr, $count) ); + var_dump( $count ); + $i++; +} -var_dump(str_replace("", "", "")); - -var_dump(str_replace("e", "b", "test")); +echo "\n*** Testing str_replace() with various subjects ***"; +$subject = "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE\000 + \x000\xABCD\0abcd \xXYZ\tabcd $$@#%^&*!~,.:;?: !!Hello, World + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)"; + +/* needles in an array to be compared in the string $string */ +$search_str = array ( + "Hello, World", + 'Hello, World', + '!!Hello, World', + "??Hello, World", + "$@#%^&*!~,.:;?", + "123", + 123, + "-1.2345", + -1.2344, + "abcd", + 'XYZ', + NULL, + "NULL", + "0", + 0, + "", + " ", + "\0", + "\x000", + "\xABC", + "\0000", + ".3", + TRUE, + "TRUE", + "1", + 1, + FALSE, + "FALSE", + " ", + " ", + 'b', + '\t', + "\t", + chr(128).chr(234).chr(65).chr(255).chr(256), + $subject +); + +/* loop through to get the $string */ +for( $i = 0; $i < count($search_str); $i++ ) { + echo "\n--- Iteration $i ---"; + echo "\n-- String after replacing the search value is => --\n"; + var_dump( str_replace($search_str[$i], "FOUND", $subject, $count) ); + echo "-- search string has found '$count' times\n"; +} + -var_dump(str_replace("", "", "", $count)); +echo "\n*** Testing Miscelleneous input data ***\n"; +/* If replace has fewer values than search, then an empty + string is used for the rest of replacement values */ +var_dump( str_replace(array("a", "a", "b"), + array("q", "q"), + "aaabb", $count + ) + ); +var_dump($count); +var_dump( str_replace(array("a", "a", "b"), + array("q", "q"), + array("aaa", "bbb", "ccc"), + $count + ) + ); var_dump($count); -var_dump(str_replace("q", "q", "q", $count)); -var_dump($count); -var_dump(str_replace("long string here", "", "", $count)); -var_dump($count); +echo "\n-- Testing objects --\n"; +/* we get "Catchable fatal error: saying Object of class could not be converted + to string" by default, when an object is passed instead of string: +The error can be avoided by chosing the __toString magix method as follows: */ + +class subject +{ + function __toString() { + return "Hello, world"; + } +} +$obj_subject = new subject; -var_dump(str_replace(chr(0), "a", "", $count)); -var_dump($count); +class search +{ + function __toString() { + return "Hello, world"; + } +} +$obj_search = new search; -var_dump(str_replace(chr(0), "a", chr(0), $count)); -var_dump($count); +class replace +{ + function __toString() { + return "Hello, world"; + } +} +$obj_replace = new replace; -var_dump(str_replace("multi", "a", "aaa", $count)); +var_dump(str_replace("$obj_search", "$obj_replace", "$obj_subject", $count)); var_dump($count); -var_dump(str_replace("a", "multi", "aaa", $count)); -var_dump($count); +echo "\n-- Testing arrays --\n"; var_dump(str_replace(array("a", "a", "b"), "multi", "aaa", $count)); var_dump($count); -var_dump(str_replace(array("a", "a", "b"), array("q", "q", "c"), "aaa", $count)); +var_dump(str_replace( array("a", "a", "b"), + array("q", "q", "c"), + "aaa", $count + ) +); var_dump($count); -var_dump(str_replace(array("a", "a", "b"), array("q", "q", "c"), array("aaa", "bbb"), $count)); +var_dump(str_replace( array("a", "a", "b"), + array("q", "q", "c"), + array("aaa", "bbb"), + $count + ) +); var_dump($count); var_dump(str_replace("a", array("q", "q", "c"), array("aaa", "bbb"), $count)); @@ -46,13 +174,70 @@ var_dump(str_replace(1, 3, array("aaa1", "2bbb"), $count)); var_dump($count); -$fp = fopen(__FILE__, "r"); -var_dump(str_replace($fp, $fp, $fp, $fp)); -var_dump($fp); + +echo "\n-- Testing Resources --\n"; +$resource1 = fopen( __FILE__, "r" ); +$resource2 = opendir( "." ); +var_dump(str_replace("stream", "FOUND", $resource1, $count)); +var_dump($count); +var_dump(str_replace("stream", "FOUND", $resource2, $count)); +var_dump($count); + + +echo "\n-- Testing a longer and heredoc string --\n"; +$string = <<<EOD +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +@#$%^&**&^%$#@!~:())))((((&&&**%$###@@@!!!~~~~@###$%^&* +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +EOD; + +var_dump( str_replace("abcdef", "FOUND", $string, $count) ); +var_dump( $count ); + +echo "\n-- Testing a heredoc null string --\n"; +$str = <<<EOD +EOD; +var_dump( str_replace("", "FOUND", $str, $count) ); +var_dump( $count ); + + +echo "\n-- Testing simple and complex syntax strings --\n"; +$str = 'world'; + +/* Simple syntax */ +var_dump( str_replace("world", "FOUND", "$str") ); +var_dump( str_replace("world'S", "FOUND", "$str'S") ); +var_dump( str_replace("worldS", "FOUND", "$strS") ); + +/* String with curly braces, complex syntax */ +var_dump( str_replace("worldS", "FOUND", "${str}S") ); +var_dump( str_replace("worldS", "FOUND", "{$str}S") ); + + +echo "\n*** Testing error conditions ***"; +/* Invalid arguments */ +var_dump( str_replace() ); +var_dump( str_replace("") ); +var_dump( str_replace(NULL) ); +var_dump( str_replace(1, 2) ); +var_dump( str_replace(1,2,3,$var,5) ); echo "Done\n"; + +--CLEAN-- +fclose($fp); +fclose($resource1); +closedir($resource2); + ?> --EXPECTF-- +*** Testing str_replace() on basic operations *** string(0) "" string(4) "tbst" string(0) "" @@ -61,102 +246,711 @@ int(1) string(0) "" int(0) -string(0) "" -int(0) -string(1) "a" +string(14) "Resource id #5" int(1) -string(3) "aaa" + +*** Testing str_replace() with various search values *** +-- Iteration 0 -- +array(12) { + [0]=> + string(5) "FOUND" + [1]=> + string(0) "" + [2]=> + string(5) "FOUND" + [3]=> + string(1) "0" + [4]=> + string(6) "-FOUND" + [5]=> + string(5) "FOUND" + [6]=> + string(1) "0" + [7]=> + string(6) "-FOUND" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" +} +int(5) + +-- Iteration 1 -- +array(12) { + [0]=> + string(1) "1" + [1]=> + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(1) "0" + [4]=> + string(2) "-1" + [5]=> + string(1) "1" + [6]=> + string(1) "0" + [7]=> + string(2) "-1" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" +} int(0) -string(15) "multimultimulti" -int(3) -string(15) "multimultimulti" -int(3) -string(3) "qqq" -int(3) -array(2) { + +-- Iteration 2 -- +array(12) { [0]=> - string(3) "qqq" + string(5) "FOUND" [1]=> - string(3) "ccc" + string(0) "" + [2]=> + string(5) "FOUND" + [3]=> + string(1) "0" + [4]=> + string(6) "-FOUND" + [5]=> + string(5) "FOUND" + [6]=> + string(1) "0" + [7]=> + string(6) "-FOUND" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" } -int(6) +int(5) -Notice: Array to string conversion in %s on line %d -array(2) { +-- Iteration 3 -- +array(12) { [0]=> - string(15) "ArrayArrayArray" + string(1) "1" [1]=> - string(3) "bbb" + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(5) "FOUND" + [4]=> + string(2) "-1" + [5]=> + string(1) "1" + [6]=> + string(5) "FOUND" + [7]=> + string(2) "-1" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" } -int(3) -array(2) { +int(2) + +-- Iteration 4 -- +array(12) { [0]=> - string(3) "111" + string(1) "1" [1]=> - string(3) "bbb" + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(1) "0" + [4]=> + string(5) "FOUND" + [5]=> + string(1) "1" + [6]=> + string(1) "0" + [7]=> + string(5) "FOUND" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" } -int(3) -array(2) { +int(2) + +-- Iteration 5 -- +array(12) { [0]=> - string(4) "aaa3" + string(5) "FOUND" [1]=> - string(4) "2bbb" + string(0) "" + [2]=> + string(5) "FOUND" + [3]=> + string(1) "0" + [4]=> + string(6) "-FOUND" + [5]=> + string(5) "FOUND" + [6]=> + string(1) "0" + [7]=> + string(6) "-FOUND" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" +} +int(5) + +-- Iteration 6 -- +array(12) { + [0]=> + string(1) "1" + [1]=> + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(5) "FOUND" + [4]=> + string(2) "-1" + [5]=> + string(1) "1" + [6]=> + string(5) "FOUND" + [7]=> + string(2) "-1" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" +} +int(2) + +-- Iteration 7 -- +array(12) { + [0]=> + string(1) "1" + [1]=> + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(1) "0" + [4]=> + string(5) "FOUND" + [5]=> + string(1) "1" + [6]=> + string(1) "0" + [7]=> + string(5) "FOUND" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" +} +int(2) + +-- Iteration 8 -- +array(12) { + [0]=> + string(1) "1" + [1]=> + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(1) "0" + [4]=> + string(2) "-1" + [5]=> + string(1) "1" + [6]=> + string(1) "0" + [7]=> + string(2) "-1" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" } -int(1) -string(%d) "Resource id #%d" -int(1) -Done ---UEXPECTF-- -unicode(0) "" -unicode(4) "tbst" -unicode(0) "" -int(0) -unicode(1) "q" -int(1) -unicode(0) "" int(0) -unicode(0) "" + +-- Iteration 9 -- +array(12) { + [0]=> + string(1) "1" + [1]=> + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(1) "0" + [4]=> + string(2) "-1" + [5]=> + string(1) "1" + [6]=> + string(1) "0" + [7]=> + string(2) "-1" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" +} int(0) -unicode(1) "a" + +-- Iteration 10 -- +array(12) { + [0]=> + string(1) "1" + [1]=> + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(1) "0" + [4]=> + string(2) "-1" + [5]=> + string(1) "1" + [6]=> + string(1) "0" + [7]=> + string(2) "-1" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(5) "FOUND" + [11]=> + string(0) "" +} int(1) -unicode(3) "aaa" + +-- Iteration 11 -- +array(12) { + [0]=> + string(1) "1" + [1]=> + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(1) "0" + [4]=> + string(2) "-1" + [5]=> + string(1) "1" + [6]=> + string(1) "0" + [7]=> + string(2) "-1" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" +} int(0) -unicode(15) "multimultimulti" -int(3) -unicode(15) "multimultimulti" + +*** Testing str_replace() with various subjects *** +--- Iteration 0 --- +-- String after replacing the search value is => -- +string(177) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?FOUND chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '2' times + +--- Iteration 1 --- +-- String after replacing the search value is => -- +string(177) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?FOUND chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '2' times + +--- Iteration 2 --- +-- String after replacing the search value is => -- +string(182) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 3 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 4 --- +-- String after replacing the search value is => -- +string(182) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 5 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 6 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 7 --- +-- String after replacing the search value is => -- +string(189) "Hello, world,0120333.3445FOUND67 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 8 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 9 --- +-- String after replacing the search value is => -- +string(193) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '2' times + +--- Iteration 10 --- +-- String after replacing the search value is => -- +string(193) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 11 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 12 --- +-- String after replacing the search value is => -- +string(192) "Hello, world,0120333.3445-1.234567 FOUND TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 13 --- +-- String after replacing the search value is => -- +string(207) "Hello, world,FOUND12FOUND333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(FOUND).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '4' times + +--- Iteration 14 --- +-- String after replacing the search value is => -- +string(207) "Hello, world,FOUND12FOUND333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(FOUND).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '4' times + +--- Iteration 15 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 16 --- +-- String after replacing the search value is => -- +string(307) "Hello,FOUNDworld,0120333.3445-1.234567FOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDNULLFOUNDTRUEFOUNDFALSE+FOUND FOUNDFOUNDFOUNDFOUND+ FOUNDFOUNDFOUNDFOUND?Hello,FOUNDWorldFOUNDchr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '29' times + +--- Iteration 17 --- +-- String after replacing the search value is => -- +string(203) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSEFOUND + FOUND0«CDFOUNDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '3' times + +--- Iteration 18 --- +-- String after replacing the search value is => -- +string(194) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ FOUND«CD+ ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 19 --- +-- String after replacing the search value is => -- +string(194) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 20 --- +-- String after replacing the search value is => -- +string(194) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ FOUND«CD+ ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 21 --- +-- String after replacing the search value is => -- +string(194) "Hello, world,0120333FOUND445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 22 --- +-- String after replacing the search value is => -- +string(203) "Hello, world,0FOUND20333.3445-FOUND.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(FOUND28).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '3' times + +--- Iteration 23 --- +-- String after replacing the search value is => -- +string(192) "Hello, world,0120333.3445-1.234567 NULL FOUND FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 24 --- +-- String after replacing the search value is => -- +string(203) "Hello, world,0FOUND20333.3445-FOUND.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(FOUND28).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '3' times + +--- Iteration 25 --- +-- String after replacing the search value is => -- +string(203) "Hello, world,0FOUND20333.3445-FOUND.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(FOUND28).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '3' times + +--- Iteration 26 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 27 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FOUND+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 28 --- +-- String after replacing the search value is => -- +string(307) "Hello,FOUNDworld,0120333.3445-1.234567FOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDNULLFOUNDTRUEFOUNDFALSE+FOUND FOUNDFOUNDFOUNDFOUND+ FOUNDFOUNDFOUNDFOUND?Hello,FOUNDWorldFOUNDchr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '29' times + +--- Iteration 29 --- +-- String after replacing the search value is => -- +string(186) "Hello, world,0120333.3445-1.234567FOUNDNULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 30 --- +-- String after replacing the search value is => -- +string(199) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '2' times + +--- Iteration 31 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 32 --- +-- String after replacing the search value is => -- +string(203) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ FOUND +FOUND ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '3' times + +--- Iteration 33 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 34 --- +-- String after replacing the search value is => -- +string(5) "FOUND" +-- search string has found '1' times + +*** Testing Miscelleneous input data *** +string(3) "qqq" +int(5) +array(3) { + [0]=> + string(3) "qqq" + [1]=> + string(0) "" + [2]=> + string(3) "ccc" +} +int(6) + +-- Testing objects -- +string(12) "Hello, world" +int(1) + +-- Testing arrays -- +string(15) "multimultimulti" int(3) -unicode(3) "qqq" +string(3) "qqq" int(3) array(2) { [0]=> - unicode(3) "qqq" + string(3) "qqq" [1]=> - unicode(3) "ccc" + string(3) "ccc" } int(6) Notice: Array to string conversion in %s on line %d array(2) { [0]=> - unicode(15) "ArrayArrayArray" + string(15) "ArrayArrayArray" [1]=> - unicode(3) "bbb" + string(3) "bbb" } int(3) array(2) { [0]=> - unicode(3) "111" + string(3) "111" [1]=> - unicode(3) "bbb" + string(3) "bbb" } int(3) array(2) { [0]=> - unicode(4) "aaa3" + string(4) "aaa3" [1]=> - unicode(4) "2bbb" + string(4) "2bbb" } int(1) -unicode(%d) "Resource id #%d" -int(1) + +-- Testing Resources -- +string(14) "Resource id #6" +int(0) +string(14) "Resource id #7" +int(0) + +-- Testing a longer and heredoc string -- +string(623) "FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789 +FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789 +FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789 +FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789 +FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789 +FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789 +FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789 +@#$%^&**&^%$#@!~:())))((((&&&**%$###@@@!!!~~~~@###$%^&* +FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789" +int(16) + +-- Testing a heredoc null string -- +string(0) "" +int(0) + +-- Testing simple and complex syntax strings -- +string(5) "FOUND" +string(5) "FOUND" + +Notice: Undefined variable: strS in %s on line %d +string(0) "" +string(5) "FOUND" +string(5) "FOUND" + +*** Testing error conditions *** +Warning: str_replace() expects at least 3 parameters, 0 given in %s on line %d +NULL + +Warning: str_replace() expects at least 3 parameters, 1 given in %s on line %d +NULL + +Warning: str_replace() expects at least 3 parameters, 1 given in %s on line %d +NULL + +Warning: str_replace() expects at least 3 parameters, 2 given in %s on line %d +NULL + +Warning: str_replace() expects at most 4 parameters, 5 given in %s on line %d +NULL Done http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/strstr.phpt?r1=1.4&r2=1.5&diff_format=u Index: php-src/ext/standard/tests/strings/strstr.phpt diff -u php-src/ext/standard/tests/strings/strstr.phpt:1.4 php-src/ext/standard/tests/strings/strstr.phpt:1.5 --- php-src/ext/standard/tests/strings/strstr.phpt:1.4 Tue Aug 16 10:18:43 2005 +++ php-src/ext/standard/tests/strings/strstr.phpt Sat May 12 12:12:26 2007 @@ -1,23 +1,181 @@ --TEST-- -strstr() function +Test strstr() function --FILE-- <?php - var_dump(strstr("test string", "test")); - var_dump(strstr("test string", "string")); - var_dump(strstr("test string", "strin")); - var_dump(strstr("test string", "t s")); - var_dump(strstr("test string", "g")); - var_dump(md5(strstr("te".chr(0)."st", chr(0)))); - var_dump(strstr("tEst", "test")); - var_dump(strstr("teSt", "test")); - var_dump(@strstr("", "")); - var_dump(@strstr("a", "")); - var_dump(@strstr("", "a")); - var_dump(md5(@strstr("\\\\a\\", "\\a"))); - var_dump(strstr("test string", " ", false)); - var_dump(strstr("test string", " ", true)); +/* Prototype: string strstr ( string $haystack, string $needle ); + Description: Find first occurrence of a string + and reurns the rest of the string from that string +*/ + +echo "*** Testing basic functionality of strstr() ***\n"; +var_dump( strstr("test string", "test") ); +var_dump( strstr("test string", "string") ); +var_dump( strstr("test string", "strin") ); +var_dump( strstr("test string", "t s") ); +var_dump( strstr("test string", "g") ); +var_dump( md5(strstr("te".chr(0)."st", chr(0))) ); +var_dump( strstr("tEst", "test") ); +var_dump( strstr("teSt", "test") ); +var_dump( @strstr("", "") ); +var_dump( @strstr("a", "") ); +var_dump( @strstr("", "a") ); + + +echo "\n*** Testing strstr() with various needles ***"; +$string = +"Hello world,012033 -3.3445 NULL TRUE FALSE\0 abcd\xxyz \x000 octal\n +abcd$:Hello world"; + +/* needles in an array to get the string starts with needle, in $string */ +$needles = array( + "Hello world", + "WORLD", + "\0", + "\x00", + "\x000", + "abcd", + "xyz", + "octal", + "-3", + -3, + "-3.344", + -3.344, + NULL, + "NULL", + "0", + 0, + TRUE, + "TRUE", + "1", + 1, + FALSE, + "FALSE", + " ", + " ", + 'b', + '\n', + "\n", + "12", + "12twelve", + $string +); + +/* loop through to get the string starts with "needle" in $string */ +for( $i = 0; $i < count($needles); $i++ ) { + echo "\n-- Iteration $i --\n"; + var_dump( strstr($string, $needles[$i]) ); +} + + +echo "\n*** Testing Miscelleneous input data ***\n"; + +echo "-- Passing objects as string and needle --\n"; +/* we get "Catchable fatal error: saying Object of class needle could not be +converted to string" by default when an object is passed instead of string: +The error can be avoided by chosing the __toString magix method as follows: */ + +class string +{ + function __toString() { + return "Hello, world"; + } +} +$obj_string = new string; + +class needle +{ + function __toString() { + return "world"; + } +} +$obj_needle = new needle; + +var_dump(strstr("$obj_string", "$obj_needle")); + + +echo "\n-- passing an array as string and needle --\n"; +$needles = array("hello", "?world", "!$%**()%**[][[[&@#~!"); +var_dump( strstr($needles, $needles) ); // won't work +var_dump( strstr("hello?world,!$%**()%**[][[[&@#~!", "$needles[1]") ); // works +var_dump( strstr("hello?world,!$%**()%**[][[[&@#~!", "$needles[2]") ); // works + + +echo "\n-- passing Resources as string and needle --\n"; +$resource1 = fopen(__FILE__, "r"); +$resource2 = opendir("."); +var_dump( strstr($resource1, $resource1) ); +var_dump( strstr($resource1, $resource2) ); + + +echo "\n-- Posiibilities with null --\n"; +var_dump( strstr("", NULL) ); +var_dump( strstr(NULL, NULL) ); +var_dump( strstr("a", NULL) ); +var_dump( strstr("/x0", "0") ); // Hexadecimal NUL + +echo "\n-- A longer and heredoc string --\n"; +$string = <<<EOD +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +EOD; +var_dump( strstr($string, "abcd") ); +var_dump( strstr($string, "1234") ); + +echo "\n-- A heredoc null string --\n"; +$str = <<<EOD +EOD; +var_dump( strstr($str, "\0") ); +var_dump( strstr($str, NULL) ); +var_dump( strstr($str, "0") ); + + +echo "\n-- simple and complex syntax strings --\n"; +$needle = 'world'; + +/* Simple syntax */ +var_dump( strstr("Hello, world", "$needle") ); // works +var_dump( strstr("Hello, world'S", "$needle'S") ); // works +var_dump( strstr("Hello, worldS", "$needleS") ); // won't work + +/* String with curly braces, complex syntax */ +var_dump( strstr("Hello, worldS", "${needle}S") ); // works +var_dump( strstr("Hello, worldS", "{$needle}S") ); // works + + +echo "\n-- complex strings containing other than 7-bit chars --\n"; +$str = chr(0).chr(128).chr(129).chr(234).chr(235).chr(254).chr(255); +echo "- Positions of some chars in the string '$str' are as follows -\n"; +echo chr(128)." => "; +var_dump( strstr($str, chr(128)) ); +echo chr(255)." => "; +var_dump( strstr($str, chr(255)) ); +echo chr(256)." => "; +var_dump( strstr($str, chr(256)) ); + +echo "\n*** Testing error conditions ***"; +var_dump( strstr($string, "")); +var_dump( strstr() ); // zero argument +var_dump( strstr("") ); // null argument +var_dump( strstr($string) ); // without "needle" +var_dump( strstr("a", "b", "c") ); // args > expected +var_dump( strstr(NULL, "") ); + +/* Cleaning the resources */ +fclose($resource1); +closedir($resource2); + +echo "\nDone"; ?> ---EXPECT-- +--EXPECTF-- +*** Testing basic functionality of strstr() *** string(11) "test string" string(6) "string" string(6) "string" @@ -29,21 +187,228 @@ bool(false) bool(false) bool(false) -string(32) "6ec19f52f0766c463f3bb240f4396913" -string(7) " string" -string(4) "test" ---UEXPECT-- -unicode(11) "test string" -unicode(6) "string" -unicode(6) "string" -unicode(8) "t string" -unicode(1) "g" -unicode(32) "7272696018bdeb2c9a3f8d01fc2a9273" + +*** Testing strstr() with various needles *** +-- Iteration 0 -- +string(86) "Hello world,012033 -3.3445 NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 1 -- bool(false) + +-- Iteration 2 -- +string(40) "+ +abcd$:Hello world" + +-- Iteration 3 -- +string(40) "+ +abcd$:Hello world" + +-- Iteration 4 -- +string(28) "+ +abcd$:Hello world" + +-- Iteration 5 -- +string(38) "abcd\xxyz + +abcd$:Hello world" + +-- Iteration 6 -- +string(32) "xyz + +abcd$:Hello world" + +-- Iteration 7 -- +string(25) "octal + +abcd$:Hello world" + +-- Iteration 8 -- +string(67) "-3.3445 NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 9 -- bool(false) + +-- Iteration 10 -- +string(67) "-3.3445 NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 11 -- bool(false) + +-- Iteration 12 -- +string(40) "+ +abcd$:Hello world" + +-- Iteration 13 -- +string(55) "NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 14 -- +string(74) "012033 -3.3445 NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 15 -- +string(40) "+ +abcd$:Hello world" + +-- Iteration 16 -- bool(false) + +-- Iteration 17 -- +string(50) "TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 18 -- +string(73) "12033 -3.3445 NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 19 -- bool(false) -unicode(32) "6ec19f52f0766c463f3bb240f4396913" -unicode(7) " string" -unicode(4) "test" + +-- Iteration 20 -- +string(40) "+ +abcd$:Hello world" + +-- Iteration 21 -- +string(45) "FALSE+ +abcd$:Hello world" + +-- Iteration 22 -- +string(81) " world,012033 -3.3445 NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 23 -- +string(60) " NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 24 -- +string(37) "bcd\xxyz + +abcd$:Hello world" + +-- Iteration 25 -- +bool(false) + +-- Iteration 26 -- +string(20) " + +abcd$:Hello world" + +-- Iteration 27 -- +string(73) "12033 -3.3445 NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 28 -- +bool(false) + +-- Iteration 29 -- +string(86) "Hello world,012033 -3.3445 NULL TRUE FALSE+ +abcd$:Hello world" + +*** Testing Miscelleneous input data *** +-- Passing objects as string and needle -- +string(5) "world" + +-- passing an array as string and needle -- + +Warning: strstr() expects parameter 1 to be string (Unicode or binary), array given in %s on line %d +NULL +string(27) "?world,!$%**()%**[][[[&@#~!" +string(20) "!$%**()%**[][[[&@#~!" + +-- passing Resources as string and needle -- + +Warning: strstr() expects parameter 1 to be string (Unicode or binary), resource given in %s on line %d +NULL + +Warning: strstr() expects parameter 1 to be string (Unicode or binary), resource given in %s on line %d +NULL + +-- Posiibilities with null -- +bool(false) +bool(false) +bool(false) +string(1) "0" + +-- A longer and heredoc string -- +string(729) "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789" +string(702) "123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789" + +-- A heredoc null string -- +bool(false) +bool(false) +bool(false) + +-- simple and complex syntax strings -- +string(5) "world" +string(7) "world'S" + +Notice: Undefined variable: needleS in %s on line %d + +Warning: strstr(): Empty delimiter in %s on line %d +bool(false) +string(6) "worldS" +string(6) "worldS" + +-- complex strings containing other than 7-bit chars -- +- Positions of some chars in the string '+ => string(6) "êëþÿ" +ÿ => string(1) "ÿ" ++ +*** Testing error conditions *** +Warning: strstr(): Empty delimiter in %s on line %d +bool(false) + +Warning: strstr() expects at least 2 parameters, 0 given in %s on line %d +NULL + +Warning: strstr() expects at least 2 parameters, 1 given in %s on line %d +NULL + +Warning: strstr() expects at least 2 parameters, 1 given in %s on line %d +NULL +bool(false) + +Warning: strstr(): Empty delimiter in %s on line %d +bool(false) + +Done
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/addcslashes.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/strings/addcslashes.phpt http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/md5_file.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/strings/md5_file.phpt http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/trim1.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/strings/trim1.phpt http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/substr.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/strings/substr.phpt diff -u /dev/null php-src/ext/standard/tests/strings/substr.phpt:1.2 --- /dev/null Sat May 12 12:12:26 2007 +++ php-src/ext/standard/tests/strings/substr.phpt Sat May 12 12:12:26 2007 @@ -0,0 +1,218 @@ +--TEST-- +Testing substr() function +--FILE-- +<?php + +/* Prototype: string substr( string str, int start[, int length] ) + * Description: Returns the portion of string specified by the start and length parameters. + */ + +$strings_array = array( NULL, "", 12345, "abcdef", "123abc", "_123abc"); + + +/* Testing for error conditions */ +echo "*** Testing for error conditions ***\n"; + +/* Zero Argument */ +var_dump( substr() ); + +/* NULL as Argument */ +var_dump( substr(NULL) ); + +/* Single Argument */ +var_dump( substr("abcde") ); + +/* Scalar Argument */ +var_dump( substr(12345) ); + +/* more than valid number of arguments ( valid are 2 or 3 ) */ +var_dump( substr("abcde", 2, 3, 4) ); + +$counter = 1; +foreach ($strings_array as $str) { + /* variations with two arguments */ + /* start values >, < and = 0 */ + + echo ("\n--- Iteration ".$counter." ---\n"); + echo ("\n-- Variations for two arguments --\n"); + var_dump ( substr($str, 1) ); + var_dump ( substr($str, 0) ); + var_dump ( substr($str, -2) ); + + /* variations with three arguments */ + /* start value variations with length values */ + + echo ("\n-- Variations for three arguments --\n"); + var_dump ( substr($str, 1, 3) ); + var_dump ( substr($str, 1, 0) ); + var_dump ( substr($str, 1, -3) ); + var_dump ( substr($str, 0, 3) ); + var_dump ( substr($str, 0, 0) ); + var_dump ( substr($str, 0, -3) ); + var_dump ( substr($str, -2, 3) ); + var_dump ( substr($str, -2, 0 ) ); + var_dump ( substr($str, -2, -3) ); + + $counter++; +} + +/* variation of start and length to point to same element */ +echo ("\n*** Testing for variations of start and length to point to same element ***\n"); +var_dump (substr("abcde" , 2, -2) ); +var_dump (substr("abcde" , -3, -2) ); + +/* Testing to return empty string when start denotes the position beyond the truncation (set by negative length) */ +echo ("\n*** Testing for start > truncation ***\n"); +var_dump (substr("abcdef" , 4, -4) ); + +/* String with null character */ +echo ("\n*** Testing for string with null characters ***\n"); +var_dump (substr("abc\x0xy\x0z" ,2) ); + +/* String with international characters */ +echo ("\n*** Testing for string with international characters ***\n"); +var_dump (substr('\xIñtërnâtiônà lizætiøn',3) ); + +echo"\nDone"; + +?> +--EXPECTF-- +*** Testing for error conditions *** + +Warning: substr() expects at least 2 parameters, 0 given in %s on line %d +NULL + +Warning: substr() expects at least 2 parameters, 1 given in %s on line %d +NULL + +Warning: substr() expects at least 2 parameters, 1 given in %s on line %d +NULL + +Warning: substr() expects at least 2 parameters, 1 given in %s on line %d +NULL + +Warning: substr() expects at most 3 parameters, 4 given in %s on line %d +NULL + +--- Iteration 1 --- + +-- Variations for two arguments -- +bool(false) +bool(false) +bool(false) + +-- Variations for three arguments -- +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) + +--- Iteration 2 --- + +-- Variations for two arguments -- +bool(false) +bool(false) +bool(false) + +-- Variations for three arguments -- +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) + +--- Iteration 3 --- + +-- Variations for two arguments -- +string(4) "2345" +string(5) "12345" +string(2) "45" + +-- Variations for three arguments -- +string(3) "234" +string(0) "" +string(1) "2" +string(3) "123" +string(0) "" +string(2) "12" +string(2) "45" +string(0) "" +string(0) "" + +--- Iteration 4 --- + +-- Variations for two arguments -- +string(5) "bcdef" +string(6) "abcdef" +string(2) "ef" + +-- Variations for three arguments -- +string(3) "bcd" +string(0) "" +string(2) "bc" +string(3) "abc" +string(0) "" +string(3) "abc" +string(2) "ef" +string(0) "" +string(0) "" + +--- Iteration 5 --- + +-- Variations for two arguments -- +string(5) "23abc" +string(6) "123abc" +string(2) "bc" + +-- Variations for three arguments -- +string(3) "23a" +string(0) "" +string(2) "23" +string(3) "123" +string(0) "" +string(3) "123" +string(2) "bc" +string(0) "" +string(0) "" + +--- Iteration 6 --- + +-- Variations for two arguments -- +string(6) "123abc" +string(7) "_123abc" +string(2) "bc" + +-- Variations for three arguments -- +string(3) "123" +string(0) "" +string(3) "123" +string(3) "_12" +string(0) "" +string(4) "_123" +string(2) "bc" +string(0) "" +string(0) "" + +*** Testing for variations of start and length to point to same element *** +string(1) "c" +string(1) "c" + +*** Testing for start > truncation *** +string(0) "" + +*** Testing for string with null characters *** +string(6) "c+ +*** Testing for string with international characters *** +string(26) "ñtërnâtiônà lizætiøn" + +Done http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/substr_count.phpt?r1=1.3&r2=1.4&diff_format=u Index: php-src/ext/standard/tests/strings/substr_count.phpt diff -u php-src/ext/standard/tests/strings/substr_count.phpt:1.3 php-src/ext/standard/tests/strings/substr_count.phpt:1.4 --- php-src/ext/standard/tests/strings/substr_count.phpt:1.3 Sat Jun 18 18:23:12 2005 +++ php-src/ext/standard/tests/strings/substr_count.phpt Sat May 12 12:12:26 2007 @@ -1,23 +1,99 @@ --TEST-- -substr_count() function +Test substr_count() function --FILE-- <?php - var_dump(@substr_count("", "")); - var_dump(@substr_count("a", "")); - var_dump(@substr_count("", "a")); - var_dump(@substr_count("", "a")); - var_dump(@substr_count("", chr(0))); +/* Prototype: int substr_count ( string $haystack, string $needle [, int $offset [, int $length]] ); + * Description: substr_count() returns the number of times the needle substring occurs in the + * haystack string. Please note that needle is case sensitive + */ + +/* Count the number of substring occurrences */ +echo "***Testing basic operations ***\n"; +var_dump(@substr_count("", "")); +var_dump(@substr_count("a", "")); +var_dump(@substr_count("", "a")); +var_dump(@substr_count("", "a")); +var_dump(@substr_count("", chr(0))); +$a = str_repeat("abcacba", 100); +var_dump(@substr_count($a, "bca")); +$a = str_repeat("abcacbabca", 100); +var_dump(@substr_count($a, "bca")); +var_dump(substr_count($a, "bca", 200)); +var_dump(substr_count($a, "bca", 200, 50)); + +echo "\n*** Testing possible variations ***\n"; +echo "-- 3rd or 4th arg as string --\n"; +$str = "this is a string"; +var_dump( substr_count($str, "t", "5") ); +var_dump( substr_count($str, "t", "5", "10") ); +var_dump( substr_count($str, "i", "5t") ); +var_dump( substr_count($str, "i", "5t", "10t") ); + +echo "\n-- 3rd or 4th arg as NULL --\n"; +var_dump( substr_count($str, "t", "") ); +var_dump( substr_count($str, "T", "") ); +var_dump( substr_count($str, "t", "", 15) ); +var_dump( substr_count($str, "I", NULL) ); +var_dump( substr_count($str, "i", NULL, 10) ); + +echo "\n-- overlapped substrings --\n"; +var_dump( substr_count("abcabcabcabcabc", "abca") ); +var_dump( substr_count("abcabcabcabcabc", "abca", 2) ); + +echo "\n-- complex strings containing other than 7-bit chars --\n"; +$str = chr(128).chr(129).chr(128).chr(256).chr(255).chr(254).chr(255); +var_dump(substr_count($str, chr(128))); +var_dump(substr_count($str, chr(255))); +var_dump(substr_count($str, chr(256))); + +echo "\n-- heredoc string --\n"; +$string = <<<EOD +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +acdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +acdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +EOD; +var_dump(substr_count($string, "abcd")); +var_dump(substr_count($string, "1234")); + +echo "\n-- heredoc null string --\n"; +$str = <<<EOD +EOD; +var_dump(substr_count($str, "\0")); +var_dump(substr_count($str, "\x000")); +var_dump(substr_count($str, "0")); + + +echo "\n*** Testing error conditions ***\n"; +/* Zero argument */ +var_dump( substr_count() ); + +/* more than expected no. of args */ +var_dump( substr_count($str, "t", 0, 15, 30) ); - $a = str_repeat("abcacba", 100); - var_dump(@substr_count($a, "bca")); +/* offset as negative value */ +var_dump(substr_count($str, "t", -5)); + +/* offset > size of the string */ +var_dump(substr_count($str, "t", 25)); + +/* Using offset and length to go beyond the size of the string: + Warning message expected, as length+offset > length of string */ +var_dump( substr_count($str, "i", 5, 15) ); + +/* length as Null */ +var_dump( substr_count($str, "t", "", "") ); +var_dump( substr_count($str, "i", NULL, NULL) ); - $a = str_repeat("abcacbabca", 100); - var_dump(@substr_count($a, "bca")); +echo "Done\n"; - var_dump(substr_count($a, "bca", 200)); - var_dump(substr_count($a, "bca", 200, 50)); ?> ---EXPECT-- +--EXPECTF-- +***Testing basic operations *** bool(false) bool(false) int(0) @@ -27,3 +103,69 @@ int(200) int(160) int(10) + +*** Testing possible variations *** +-- 3rd or 4th arg as string -- +int(1) +int(1) + +Notice: A non well formed numeric value encountered in %s on line %d +int(2) + +Notice: A non well formed numeric value encountered in %s on line %d + +Notice: A non well formed numeric value encountered in %s on line %d +int(2) + +-- 3rd or 4th arg as NULL -- + +Warning: substr_count() expects parameter 3 to be long, string given in %s on line %d +NULL + +Warning: substr_count() expects parameter 3 to be long, string given in %s on line %d +NULL + +Warning: substr_count() expects parameter 3 to be long, string given in %s on line %d +NULL +int(0) +int(2) + +-- overlapped substrings -- +int(2) +int(2) + +-- complex strings containing other than 7-bit chars -- +int(2) +int(2) +int(1) + +-- heredoc string -- +int(14) +int(16) + +-- heredoc null string -- +int(0) +int(0) +int(0) + +*** Testing error conditions *** + +Warning: substr_count() expects at least 2 parameters, 0 given in %s on line %d +NULL + +Warning: substr_count() expects at most 4 parameters, 5 given in %s on line %d +NULL + +Warning: substr_count(): Offset should be greater than or equal to 0 in %s on line %d +bool(false) + +Warning: substr_count(): Offset value 25 exceeds string length in %s on line %d +bool(false) + +Warning: substr_count(): Offset value 5 exceeds string length in %s on line %d +bool(false) + +Warning: substr_count() expects parameter 3 to be long, string given in %s on line %d +NULL +int(0) +Done http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/implode1.phpt?r1=1.1&r2=1.2&diff_format=u Index: php-src/ext/standard/tests/strings/implode1.phpt http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/str_repeat.phpt?r1=1.2&r2=1.3&diff_format=u Index: php-src/ext/standard/tests/strings/str_repeat.phpt http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/strpos.phpt?r1=1.2&r2=1.3&diff_format=u Index: php-src/ext/standard/tests/strings/strpos.phpt diff -u php-src/ext/standard/tests/strings/strpos.phpt:1.2 php-src/ext/standard/tests/strings/strpos.phpt:1.3 --- php-src/ext/standard/tests/strings/strpos.phpt:1.2 Wed May 19 08:45:23 2004 +++ php-src/ext/standard/tests/strings/strpos.phpt Sat May 12 12:12:26 2007 @@ -1,21 +1,199 @@ --TEST-- -strpos() function +Test strpos() function --FILE-- <?php - var_dump(strpos("test string", "test")); - var_dump(strpos("test string", "string")); - var_dump(strpos("test string", "strin")); - var_dump(strpos("test string", "t s")); - var_dump(strpos("test string", "g")); - var_dump(strpos("te".chr(0)."st", chr(0))); - var_dump(strpos("tEst", "test")); - var_dump(strpos("teSt", "test")); - var_dump(@strpos("", "")); - var_dump(@strpos("a", "")); - var_dump(@strpos("", "a")); - var_dump(@strpos("\\\\a", "\\a")); +/* Prototype: int strpos ( string $haystack, mixed $needle [, int $offset] ); + Description: Find position of first occurrence of a string +*/ + +echo "*** Testing basic functionality of strpos() ***\n"; +var_dump( strpos("test string", "test") ); +var_dump( strpos("test string", "string") ); +var_dump( strpos("test string", "strin") ); +var_dump( strpos("test string", "t s") ); +var_dump( strpos("test string", "g") ); +var_dump( strpos("te".chr(0)."st", chr(0)) ); +var_dump( strpos("tEst", "test") ); +var_dump( strpos("teSt", "test") ); +var_dump( @strpos("", "") ); +var_dump( @strpos("a", "") ); +var_dump( @strpos("", "a") ); +var_dump( @strpos("\\\\a", "\\a") ); + + +echo "\n*** Testing stropos() to find various needles and a long string ***\n"; +$string = +"Hello world,012033 -3.3445 NULL TRUE FALSE\0 abcd\xxyz \x000 octal\n +abcd$:Hello world"; + +/* needles in an array to get the position of needle in $string */ +$needles = array( + "Hello world", + "WORLD", + "\0", + "\x00", + "\x000", + "abcd", + "xyz", + "octal", + "-3", + -3, + "-3.344", + -3.344, + NULL, + "NULL", + "0", + 0, + TRUE, + "TRUE", + "1", + 1, + FALSE, + "FALSE", + " ", + " ", + 'b', + '\n', + "\n", + "12", + "12twelve", + $string +); + +/* loop through to get the "needle" position in $string */ +for( $i = 0; $i < count($needles); $i++ ) { + echo "Position of '$needles[$i]' is => "; + var_dump( strpos($string, $needles[$i]) ); +} + + +echo "\n*** Testing strpos() with possible variations in offset ***\n"; +$offset_values = array ( + 1, // offset = 1 + NULL, // offset as string, converts to zero + TRUE, + NULL, + FALSE, +); + +/* loop through to get the "needle" position in $string */ +for( $i = 0; $i < count( $offset_values ); $i++ ) { + echo "Position of 'Hello' with offset '$offset_values[$i]' is => "; + var_dump( strpos($string, "Hello", $offset_values[$i]) ); +} + + +echo "\n*** Testing Miscelleneous input data ***\n"; + +echo "-- Passing objects as string and needle --\n"; +/* we get "Catchable fatal error: saying Object of class needle could not be + converted to string" by default when an object is passed instead of string: + The error can be avoided by chosing the __toString magix method as follows: */ + +class string +{ + function __toString() { + return "Hello, world"; + } +} +$obj_string = new string; + +class needle +{ + function __toString() { + return "world"; + } +} +$obj_needle = new needle; + +var_dump( strpos("$obj_string", "$obj_needle") ); + +echo "\n-- Passing an array as string and needle --\n"; +$needles = array("hello", "?world", "!$%**()%**[][[[&@#~!"); +var_dump( strpos($needles, $needles) ); // won't work +var_dump( strpos("hello?world,!$%**()%**[][[[&@#~!", "$needles[1]") ); // works +var_dump( strpos("hello?world,!$%**()%**[][[[&@#~!", "$needles[2]") ); // works + + +echo "\n-- Passing Resources as string and needle --\n"; +$resource1 = fopen(__FILE__, "r"); +$resource2 = opendir("."); +var_dump( strpos($resource1, $resource1) ); +var_dump( strpos($resource1, $resource2) ); + +echo "\n-- Posiibilities with null --\n"; +var_dump( strpos("", NULL) ); +var_dump( strpos(NULL, NULL) ); +var_dump( strpos("a", NULL) ); +var_dump( strpos("/x0", "0") ); // Hexadecimal NUL + +echo "\n-- A longer and heredoc string --\n"; +$string = <<<EOD +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +EOD; +var_dump( strpos($string, "abcd") ); +var_dump( strpos($string, "abcd", 72) ); // 72 -> "\n" in the first line +var_dump( strpos($string, "abcd", 73) ); // 73 -> "abcd" in the second line +var_dump( strpos($string, "9", (strlen($string)-1)) ); + +echo "\n-- A heredoc null string --\n"; +$str = <<<EOD +EOD; +var_dump( strpos($str, "\0") ); +var_dump( strpos($str, NULL) ); +var_dump( strpos($str, "0") ); + + +echo "\n-- simple and complex syntax strings --\n"; +$needle = 'world'; + +/* Simple syntax */ +var_dump( strpos("Hello, world", "$needle") ); // works +var_dump( strpos("Hello, world'S", "$needle'S") ); // works +var_dump( strpos("Hello, worldS", "$needleS") ); // won't work + +/* String with curly braces, complex syntax */ +var_dump( strpos("Hello, worldS", "${needle}S") ); // works +var_dump( strpos("Hello, worldS", "{$needle}S") ); // works + + +echo "\n-- complex strings containing other than 7-bit chars --\n"; +$str = chr(0).chr(128).chr(129).chr(234).chr(235).chr(254).chr(255); +echo "-- Positions of some chars in the string '$str' are as follows --\n"; +echo chr(128)." => "; +var_dump( strpos($str, chr(128)) ); +echo chr(255)." => "; +var_dump( strpos($str, chr(255), 3) ); +echo chr(256)." => "; +var_dump( strpos($str, chr(256)) ); + +echo "\n*** Testing error conditions ***"; +var_dump( strpos($string, "") ); +var_dump( strpos() ); // zero argument +var_dump( strpos("") ); // null argument +var_dump( strpos($string) ); // without "needle" +var_dump( strpos("a", "b", "c") ); // string as offset +var_dump( strpos("a", "b", "c", "d") ); // args > expected +var_dump( strpos($string, "test", strlen($string)+1) ); // offset > strlen() +var_dump( strpos($string, "test", -1) ); // offset < 0 +var_dump( strpos(NULL, "") ); + +fclose($resource1); +closedir($resource2); + +echo "\nDone"; ?> ---EXPECT-- +--EXPECTF-- +*** Testing basic functionality of strpos() *** int(0) int(5) int(5) @@ -28,3 +206,128 @@ bool(false) bool(false) int(1) + +*** Testing stropos() to find various needles and a long string *** +Position of 'Hello world' is => int(0) +Position of 'WORLD' is => bool(false) +Position of '+Position of '+Position of '+Position of 'abcd' is => int(48) +Position of 'xyz' is => int(54) +Position of 'octal' is => int(61) +Position of '-3' is => int(19) +Position of '-3' is => bool(false) +Position of '-3.344' is => int(19) +Position of '-3.344' is => bool(false) +Position of '' is => int(46) +Position of 'NULL' is => int(31) +Position of '0' is => int(12) +Position of '0' is => int(46) +Position of '1' is => bool(false) +Position of 'TRUE' is => int(36) +Position of '1' is => int(13) +Position of '1' is => bool(false) +Position of '' is => int(46) +Position of 'FALSE' is => int(41) +Position of ' ' is => int(5) +Position of ' ' is => int(26) +Position of 'b' is => int(49) +Position of '\n' is => bool(false) +Position of ' +' is => int(66) +Position of '12' is => int(13) +Position of '12twelve' is => bool(false) +Position of 'Hello world,012033 -3.3445 NULL TRUE FALSE+ +abcd$:Hello world' is => int(0) + +*** Testing strpos() with possible variations in offset *** +Position of 'Hello' with offset '1' is => int(74) +Position of 'Hello' with offset '' is => int(0) +Position of 'Hello' with offset '1' is => int(74) +Position of 'Hello' with offset '' is => int(0) +Position of 'Hello' with offset '' is => int(0) + +*** Testing Miscelleneous input data *** +-- Passing objects as string and needle -- +int(7) + +-- Passing an array as string and needle -- + +Warning: strpos() expects parameter 1 to be string (Unicode or binary), array given in %s on line 110 +NULL +int(5) +int(12) + +-- Passing Resources as string and needle -- + +Warning: strpos() expects parameter 1 to be string (Unicode or binary), resource given in %s on line 118 +NULL + +Warning: strpos() expects parameter 1 to be string (Unicode or binary), resource given in %s on line 119 +NULL + +-- Posiibilities with null -- +bool(false) +bool(false) +bool(false) +int(2) + +-- A longer and heredoc string -- +int(0) +int(73) +int(73) +int(728) + +-- A heredoc null string -- +bool(false) +bool(false) +bool(false) + +-- simple and complex syntax strings -- +int(7) +int(7) + +Notice: Undefined variable: needleS in %s on line 159 + +Warning: strpos(): Empty delimiter in %s on line 159 +bool(false) +int(7) +int(7) + +-- complex strings containing other than 7-bit chars -- +-- Positions of some chars in the string '+ => int(1) +ÿ => int(6) ++ +*** Testing error conditions *** +Warning: strpos(): Empty delimiter in %s on line %d +bool(false) + +Warning: strpos() expects at least 2 parameters, 0 given in %s on line %d +NULL + +Warning: strpos() expects at least 2 parameters, 1 given in %s on line %d +NULL + +Warning: strpos() expects at least 2 parameters, 1 given in %s on line %d +NULL + +Warning: strpos() expects parameter 3 to be long, string given in %s on line %d +NULL + +Warning: strpos() expects at most 3 parameters, 4 given in %s on line %d +NULL + +Warning: strpos(): Offset not contained in string in %s on line %d +bool(false) + +Warning: strpos(): Offset not contained in string in %s on line %d +bool(false) + +Warning: strpos(): Empty delimiter in %s on line %d +bool(false) + +Done http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/chr_ord.phpt?r1=1.2&r2=1.3&diff_format=u Index: php-src/ext/standard/tests/strings/chr_ord.phpt http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/str_replace.phpt?r1=1.2&r2=1.3&diff_format=u Index: php-src/ext/standard/tests/strings/str_replace.phpt diff -u php-src/ext/standard/tests/strings/str_replace.phpt:1.2 php-src/ext/standard/tests/strings/str_replace.phpt:1.3 --- php-src/ext/standard/tests/strings/str_replace.phpt:1.2 Thu Nov 9 01:16:48 2006 +++ php-src/ext/standard/tests/strings/str_replace.phpt Sat May 12 12:12:26 2007 @@ -1,40 +1,168 @@ --TEST-- -str_replace() tests +Test str_replace() function --FILE-- <?php +/* + Prototype: mixed str_replace(mixed $search, mixed $replace, + mixed $subject [, int &$count]); + Description: Replace all occurrences of the search string with + the replacement string +*/ + +echo "\n*** Testing str_replace() on basic operations ***\n"; + +var_dump( str_replace("", "", "") ); + +var_dump( str_replace("e", "b", "test") ); + +var_dump( str_replace("", "", "", $count) ); +var_dump( $count ); + +var_dump( str_replace("q", "q", "q", $count) ); +var_dump( $count ); + +var_dump( str_replace("long string here", "", "", $count) ); +var_dump( $count ); + +$fp = fopen( __FILE__, "r" ); +var_dump( str_replace($fp, $fp, $fp, $fp) ); +var_dump( $fp ); + +echo "\n*** Testing str_replace() with various search values ***"; +$search_arr = array( TRUE, FALSE, 1, 0, -1, "1", "0", "-1", NULL, + array(), "php", ""); + +$i = 0; +/* loop through to replace the matched elements in the array */ +foreach( $search_arr as $value ) { + echo "\n-- Iteration $i --\n"; + /* replace the string in array */ + var_dump( str_replace($value, "FOUND", $search_arr, $count) ); + var_dump( $count ); + $i++; +} -var_dump(str_replace("", "", "")); - -var_dump(str_replace("e", "b", "test")); +echo "\n*** Testing str_replace() with various subjects ***"; +$subject = "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE\000 + \x000\xABCD\0abcd \xXYZ\tabcd $$@#%^&*!~,.:;?: !!Hello, World + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)"; + +/* needles in an array to be compared in the string $string */ +$search_str = array ( + "Hello, World", + 'Hello, World', + '!!Hello, World', + "??Hello, World", + "$@#%^&*!~,.:;?", + "123", + 123, + "-1.2345", + -1.2344, + "abcd", + 'XYZ', + NULL, + "NULL", + "0", + 0, + "", + " ", + "\0", + "\x000", + "\xABC", + "\0000", + ".3", + TRUE, + "TRUE", + "1", + 1, + FALSE, + "FALSE", + " ", + " ", + 'b', + '\t', + "\t", + chr(128).chr(234).chr(65).chr(255).chr(256), + $subject +); + +/* loop through to get the $string */ +for( $i = 0; $i < count($search_str); $i++ ) { + echo "\n--- Iteration $i ---"; + echo "\n-- String after replacing the search value is => --\n"; + var_dump( str_replace($search_str[$i], "FOUND", $subject, $count) ); + echo "-- search string has found '$count' times\n"; +} + -var_dump(str_replace("", "", "", $count)); +echo "\n*** Testing Miscelleneous input data ***\n"; +/* If replace has fewer values than search, then an empty + string is used for the rest of replacement values */ +var_dump( str_replace(array("a", "a", "b"), + array("q", "q"), + "aaabb", $count + ) + ); +var_dump($count); +var_dump( str_replace(array("a", "a", "b"), + array("q", "q"), + array("aaa", "bbb", "ccc"), + $count + ) + ); var_dump($count); -var_dump(str_replace("q", "q", "q", $count)); -var_dump($count); -var_dump(str_replace("long string here", "", "", $count)); -var_dump($count); +echo "\n-- Testing objects --\n"; +/* we get "Catchable fatal error: saying Object of class could not be converted + to string" by default, when an object is passed instead of string: +The error can be avoided by chosing the __toString magix method as follows: */ + +class subject +{ + function __toString() { + return "Hello, world"; + } +} +$obj_subject = new subject; -var_dump(str_replace(chr(0), "a", "", $count)); -var_dump($count); +class search +{ + function __toString() { + return "Hello, world"; + } +} +$obj_search = new search; -var_dump(str_replace(chr(0), "a", chr(0), $count)); -var_dump($count); +class replace +{ + function __toString() { + return "Hello, world"; + } +} +$obj_replace = new replace; -var_dump(str_replace("multi", "a", "aaa", $count)); +var_dump(str_replace("$obj_search", "$obj_replace", "$obj_subject", $count)); var_dump($count); -var_dump(str_replace("a", "multi", "aaa", $count)); -var_dump($count); +echo "\n-- Testing arrays --\n"; var_dump(str_replace(array("a", "a", "b"), "multi", "aaa", $count)); var_dump($count); -var_dump(str_replace(array("a", "a", "b"), array("q", "q", "c"), "aaa", $count)); +var_dump(str_replace( array("a", "a", "b"), + array("q", "q", "c"), + "aaa", $count + ) +); var_dump($count); -var_dump(str_replace(array("a", "a", "b"), array("q", "q", "c"), array("aaa", "bbb"), $count)); +var_dump(str_replace( array("a", "a", "b"), + array("q", "q", "c"), + array("aaa", "bbb"), + $count + ) +); var_dump($count); var_dump(str_replace("a", array("q", "q", "c"), array("aaa", "bbb"), $count)); @@ -46,13 +174,70 @@ var_dump(str_replace(1, 3, array("aaa1", "2bbb"), $count)); var_dump($count); -$fp = fopen(__FILE__, "r"); -var_dump(str_replace($fp, $fp, $fp, $fp)); -var_dump($fp); + +echo "\n-- Testing Resources --\n"; +$resource1 = fopen( __FILE__, "r" ); +$resource2 = opendir( "." ); +var_dump(str_replace("stream", "FOUND", $resource1, $count)); +var_dump($count); +var_dump(str_replace("stream", "FOUND", $resource2, $count)); +var_dump($count); + + +echo "\n-- Testing a longer and heredoc string --\n"; +$string = <<<EOD +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +@#$%^&**&^%$#@!~:())))((((&&&**%$###@@@!!!~~~~@###$%^&* +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +EOD; + +var_dump( str_replace("abcdef", "FOUND", $string, $count) ); +var_dump( $count ); + +echo "\n-- Testing a heredoc null string --\n"; +$str = <<<EOD +EOD; +var_dump( str_replace("", "FOUND", $str, $count) ); +var_dump( $count ); + + +echo "\n-- Testing simple and complex syntax strings --\n"; +$str = 'world'; + +/* Simple syntax */ +var_dump( str_replace("world", "FOUND", "$str") ); +var_dump( str_replace("world'S", "FOUND", "$str'S") ); +var_dump( str_replace("worldS", "FOUND", "$strS") ); + +/* String with curly braces, complex syntax */ +var_dump( str_replace("worldS", "FOUND", "${str}S") ); +var_dump( str_replace("worldS", "FOUND", "{$str}S") ); + + +echo "\n*** Testing error conditions ***"; +/* Invalid arguments */ +var_dump( str_replace() ); +var_dump( str_replace("") ); +var_dump( str_replace(NULL) ); +var_dump( str_replace(1, 2) ); +var_dump( str_replace(1,2,3,$var,5) ); echo "Done\n"; + +--CLEAN-- +fclose($fp); +fclose($resource1); +closedir($resource2); + ?> --EXPECTF-- +*** Testing str_replace() on basic operations *** string(0) "" string(4) "tbst" string(0) "" @@ -61,102 +246,711 @@ int(1) string(0) "" int(0) -string(0) "" -int(0) -string(1) "a" +string(14) "Resource id #5" int(1) -string(3) "aaa" + +*** Testing str_replace() with various search values *** +-- Iteration 0 -- +array(12) { + [0]=> + string(5) "FOUND" + [1]=> + string(0) "" + [2]=> + string(5) "FOUND" + [3]=> + string(1) "0" + [4]=> + string(6) "-FOUND" + [5]=> + string(5) "FOUND" + [6]=> + string(1) "0" + [7]=> + string(6) "-FOUND" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" +} +int(5) + +-- Iteration 1 -- +array(12) { + [0]=> + string(1) "1" + [1]=> + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(1) "0" + [4]=> + string(2) "-1" + [5]=> + string(1) "1" + [6]=> + string(1) "0" + [7]=> + string(2) "-1" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" +} int(0) -string(15) "multimultimulti" -int(3) -string(15) "multimultimulti" -int(3) -string(3) "qqq" -int(3) -array(2) { + +-- Iteration 2 -- +array(12) { [0]=> - string(3) "qqq" + string(5) "FOUND" [1]=> - string(3) "ccc" + string(0) "" + [2]=> + string(5) "FOUND" + [3]=> + string(1) "0" + [4]=> + string(6) "-FOUND" + [5]=> + string(5) "FOUND" + [6]=> + string(1) "0" + [7]=> + string(6) "-FOUND" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" } -int(6) +int(5) -Notice: Array to string conversion in %s on line %d -array(2) { +-- Iteration 3 -- +array(12) { [0]=> - string(15) "ArrayArrayArray" + string(1) "1" [1]=> - string(3) "bbb" + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(5) "FOUND" + [4]=> + string(2) "-1" + [5]=> + string(1) "1" + [6]=> + string(5) "FOUND" + [7]=> + string(2) "-1" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" } -int(3) -array(2) { +int(2) + +-- Iteration 4 -- +array(12) { [0]=> - string(3) "111" + string(1) "1" [1]=> - string(3) "bbb" + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(1) "0" + [4]=> + string(5) "FOUND" + [5]=> + string(1) "1" + [6]=> + string(1) "0" + [7]=> + string(5) "FOUND" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" } -int(3) -array(2) { +int(2) + +-- Iteration 5 -- +array(12) { [0]=> - string(4) "aaa3" + string(5) "FOUND" [1]=> - string(4) "2bbb" + string(0) "" + [2]=> + string(5) "FOUND" + [3]=> + string(1) "0" + [4]=> + string(6) "-FOUND" + [5]=> + string(5) "FOUND" + [6]=> + string(1) "0" + [7]=> + string(6) "-FOUND" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" +} +int(5) + +-- Iteration 6 -- +array(12) { + [0]=> + string(1) "1" + [1]=> + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(5) "FOUND" + [4]=> + string(2) "-1" + [5]=> + string(1) "1" + [6]=> + string(5) "FOUND" + [7]=> + string(2) "-1" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" +} +int(2) + +-- Iteration 7 -- +array(12) { + [0]=> + string(1) "1" + [1]=> + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(1) "0" + [4]=> + string(5) "FOUND" + [5]=> + string(1) "1" + [6]=> + string(1) "0" + [7]=> + string(5) "FOUND" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" +} +int(2) + +-- Iteration 8 -- +array(12) { + [0]=> + string(1) "1" + [1]=> + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(1) "0" + [4]=> + string(2) "-1" + [5]=> + string(1) "1" + [6]=> + string(1) "0" + [7]=> + string(2) "-1" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" } -int(1) -string(%d) "Resource id #%d" -int(1) -Done ---UEXPECTF-- -unicode(0) "" -unicode(4) "tbst" -unicode(0) "" -int(0) -unicode(1) "q" -int(1) -unicode(0) "" int(0) -unicode(0) "" + +-- Iteration 9 -- +array(12) { + [0]=> + string(1) "1" + [1]=> + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(1) "0" + [4]=> + string(2) "-1" + [5]=> + string(1) "1" + [6]=> + string(1) "0" + [7]=> + string(2) "-1" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" +} int(0) -unicode(1) "a" + +-- Iteration 10 -- +array(12) { + [0]=> + string(1) "1" + [1]=> + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(1) "0" + [4]=> + string(2) "-1" + [5]=> + string(1) "1" + [6]=> + string(1) "0" + [7]=> + string(2) "-1" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(5) "FOUND" + [11]=> + string(0) "" +} int(1) -unicode(3) "aaa" + +-- Iteration 11 -- +array(12) { + [0]=> + string(1) "1" + [1]=> + string(0) "" + [2]=> + string(1) "1" + [3]=> + string(1) "0" + [4]=> + string(2) "-1" + [5]=> + string(1) "1" + [6]=> + string(1) "0" + [7]=> + string(2) "-1" + [8]=> + string(0) "" + [9]=> + array(0) { + } + [10]=> + string(3) "php" + [11]=> + string(0) "" +} int(0) -unicode(15) "multimultimulti" -int(3) -unicode(15) "multimultimulti" + +*** Testing str_replace() with various subjects *** +--- Iteration 0 --- +-- String after replacing the search value is => -- +string(177) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?FOUND chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '2' times + +--- Iteration 1 --- +-- String after replacing the search value is => -- +string(177) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?FOUND chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '2' times + +--- Iteration 2 --- +-- String after replacing the search value is => -- +string(182) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 3 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 4 --- +-- String after replacing the search value is => -- +string(182) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 5 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 6 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 7 --- +-- String after replacing the search value is => -- +string(189) "Hello, world,0120333.3445FOUND67 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 8 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 9 --- +-- String after replacing the search value is => -- +string(193) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '2' times + +--- Iteration 10 --- +-- String after replacing the search value is => -- +string(193) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 11 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 12 --- +-- String after replacing the search value is => -- +string(192) "Hello, world,0120333.3445-1.234567 FOUND TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 13 --- +-- String after replacing the search value is => -- +string(207) "Hello, world,FOUND12FOUND333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(FOUND).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '4' times + +--- Iteration 14 --- +-- String after replacing the search value is => -- +string(207) "Hello, world,FOUND12FOUND333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(FOUND).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '4' times + +--- Iteration 15 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 16 --- +-- String after replacing the search value is => -- +string(307) "Hello,FOUNDworld,0120333.3445-1.234567FOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDNULLFOUNDTRUEFOUNDFALSE+FOUND FOUNDFOUNDFOUNDFOUND+ FOUNDFOUNDFOUNDFOUND?Hello,FOUNDWorldFOUNDchr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '29' times + +--- Iteration 17 --- +-- String after replacing the search value is => -- +string(203) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSEFOUND + FOUND0«CDFOUNDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '3' times + +--- Iteration 18 --- +-- String after replacing the search value is => -- +string(194) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ FOUND«CD+ ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 19 --- +-- String after replacing the search value is => -- +string(194) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 20 --- +-- String after replacing the search value is => -- +string(194) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ FOUND«CD+ ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 21 --- +-- String after replacing the search value is => -- +string(194) "Hello, world,0120333FOUND445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 22 --- +-- String after replacing the search value is => -- +string(203) "Hello, world,0FOUND20333.3445-FOUND.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(FOUND28).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '3' times + +--- Iteration 23 --- +-- String after replacing the search value is => -- +string(192) "Hello, world,0120333.3445-1.234567 NULL FOUND FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 24 --- +-- String after replacing the search value is => -- +string(203) "Hello, world,0FOUND20333.3445-FOUND.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(FOUND28).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '3' times + +--- Iteration 25 --- +-- String after replacing the search value is => -- +string(203) "Hello, world,0FOUND20333.3445-FOUND.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(FOUND28).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '3' times + +--- Iteration 26 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 27 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FOUND+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 28 --- +-- String after replacing the search value is => -- +string(307) "Hello,FOUNDworld,0120333.3445-1.234567FOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDNULLFOUNDTRUEFOUNDFALSE+FOUND FOUNDFOUNDFOUNDFOUND+ FOUNDFOUNDFOUNDFOUND?Hello,FOUNDWorldFOUNDchr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '29' times + +--- Iteration 29 --- +-- String after replacing the search value is => -- +string(186) "Hello, world,0120333.3445-1.234567FOUNDNULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '1' times + +--- Iteration 30 --- +-- String after replacing the search value is => -- +string(199) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '2' times + +--- Iteration 31 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 32 --- +-- String after replacing the search value is => -- +string(203) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ FOUND +FOUND ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '3' times + +--- Iteration 33 --- +-- String after replacing the search value is => -- +string(191) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE+ + ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" +-- search string has found '0' times + +--- Iteration 34 --- +-- String after replacing the search value is => -- +string(5) "FOUND" +-- search string has found '1' times + +*** Testing Miscelleneous input data *** +string(3) "qqq" +int(5) +array(3) { + [0]=> + string(3) "qqq" + [1]=> + string(0) "" + [2]=> + string(3) "ccc" +} +int(6) + +-- Testing objects -- +string(12) "Hello, world" +int(1) + +-- Testing arrays -- +string(15) "multimultimulti" int(3) -unicode(3) "qqq" +string(3) "qqq" int(3) array(2) { [0]=> - unicode(3) "qqq" + string(3) "qqq" [1]=> - unicode(3) "ccc" + string(3) "ccc" } int(6) Notice: Array to string conversion in %s on line %d array(2) { [0]=> - unicode(15) "ArrayArrayArray" + string(15) "ArrayArrayArray" [1]=> - unicode(3) "bbb" + string(3) "bbb" } int(3) array(2) { [0]=> - unicode(3) "111" + string(3) "111" [1]=> - unicode(3) "bbb" + string(3) "bbb" } int(3) array(2) { [0]=> - unicode(4) "aaa3" + string(4) "aaa3" [1]=> - unicode(4) "2bbb" + string(4) "2bbb" } int(1) -unicode(%d) "Resource id #%d" -int(1) + +-- Testing Resources -- +string(14) "Resource id #6" +int(0) +string(14) "Resource id #7" +int(0) + +-- Testing a longer and heredoc string -- +string(623) "FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789 +FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789 +FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789 +FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789 +FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789 +FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789 +FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789 +@#$%^&**&^%$#@!~:())))((((&&&**%$###@@@!!!~~~~@###$%^&* +FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789" +int(16) + +-- Testing a heredoc null string -- +string(0) "" +int(0) + +-- Testing simple and complex syntax strings -- +string(5) "FOUND" +string(5) "FOUND" + +Notice: Undefined variable: strS in %s on line %d +string(0) "" +string(5) "FOUND" +string(5) "FOUND" + +*** Testing error conditions *** +Warning: str_replace() expects at least 3 parameters, 0 given in %s on line %d +NULL + +Warning: str_replace() expects at least 3 parameters, 1 given in %s on line %d +NULL + +Warning: str_replace() expects at least 3 parameters, 1 given in %s on line %d +NULL + +Warning: str_replace() expects at least 3 parameters, 2 given in %s on line %d +NULL + +Warning: str_replace() expects at most 4 parameters, 5 given in %s on line %d +NULL Done http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/strstr.phpt?r1=1.4&r2=1.5&diff_format=u Index: php-src/ext/standard/tests/strings/strstr.phpt diff -u php-src/ext/standard/tests/strings/strstr.phpt:1.4 php-src/ext/standard/tests/strings/strstr.phpt:1.5 --- php-src/ext/standard/tests/strings/strstr.phpt:1.4 Tue Aug 16 10:18:43 2005 +++ php-src/ext/standard/tests/strings/strstr.phpt Sat May 12 12:12:26 2007 @@ -1,23 +1,181 @@ --TEST-- -strstr() function +Test strstr() function --FILE-- <?php - var_dump(strstr("test string", "test")); - var_dump(strstr("test string", "string")); - var_dump(strstr("test string", "strin")); - var_dump(strstr("test string", "t s")); - var_dump(strstr("test string", "g")); - var_dump(md5(strstr("te".chr(0)."st", chr(0)))); - var_dump(strstr("tEst", "test")); - var_dump(strstr("teSt", "test")); - var_dump(@strstr("", "")); - var_dump(@strstr("a", "")); - var_dump(@strstr("", "a")); - var_dump(md5(@strstr("\\\\a\\", "\\a"))); - var_dump(strstr("test string", " ", false)); - var_dump(strstr("test string", " ", true)); +/* Prototype: string strstr ( string $haystack, string $needle ); + Description: Find first occurrence of a string + and reurns the rest of the string from that string +*/ + +echo "*** Testing basic functionality of strstr() ***\n"; +var_dump( strstr("test string", "test") ); +var_dump( strstr("test string", "string") ); +var_dump( strstr("test string", "strin") ); +var_dump( strstr("test string", "t s") ); +var_dump( strstr("test string", "g") ); +var_dump( md5(strstr("te".chr(0)."st", chr(0))) ); +var_dump( strstr("tEst", "test") ); +var_dump( strstr("teSt", "test") ); +var_dump( @strstr("", "") ); +var_dump( @strstr("a", "") ); +var_dump( @strstr("", "a") ); + + +echo "\n*** Testing strstr() with various needles ***"; +$string = +"Hello world,012033 -3.3445 NULL TRUE FALSE\0 abcd\xxyz \x000 octal\n +abcd$:Hello world"; + +/* needles in an array to get the string starts with needle, in $string */ +$needles = array( + "Hello world", + "WORLD", + "\0", + "\x00", + "\x000", + "abcd", + "xyz", + "octal", + "-3", + -3, + "-3.344", + -3.344, + NULL, + "NULL", + "0", + 0, + TRUE, + "TRUE", + "1", + 1, + FALSE, + "FALSE", + " ", + " ", + 'b', + '\n', + "\n", + "12", + "12twelve", + $string +); + +/* loop through to get the string starts with "needle" in $string */ +for( $i = 0; $i < count($needles); $i++ ) { + echo "\n-- Iteration $i --\n"; + var_dump( strstr($string, $needles[$i]) ); +} + + +echo "\n*** Testing Miscelleneous input data ***\n"; + +echo "-- Passing objects as string and needle --\n"; +/* we get "Catchable fatal error: saying Object of class needle could not be +converted to string" by default when an object is passed instead of string: +The error can be avoided by chosing the __toString magix method as follows: */ + +class string +{ + function __toString() { + return "Hello, world"; + } +} +$obj_string = new string; + +class needle +{ + function __toString() { + return "world"; + } +} +$obj_needle = new needle; + +var_dump(strstr("$obj_string", "$obj_needle")); + + +echo "\n-- passing an array as string and needle --\n"; +$needles = array("hello", "?world", "!$%**()%**[][[[&@#~!"); +var_dump( strstr($needles, $needles) ); // won't work +var_dump( strstr("hello?world,!$%**()%**[][[[&@#~!", "$needles[1]") ); // works +var_dump( strstr("hello?world,!$%**()%**[][[[&@#~!", "$needles[2]") ); // works + + +echo "\n-- passing Resources as string and needle --\n"; +$resource1 = fopen(__FILE__, "r"); +$resource2 = opendir("."); +var_dump( strstr($resource1, $resource1) ); +var_dump( strstr($resource1, $resource2) ); + + +echo "\n-- Posiibilities with null --\n"; +var_dump( strstr("", NULL) ); +var_dump( strstr(NULL, NULL) ); +var_dump( strstr("a", NULL) ); +var_dump( strstr("/x0", "0") ); // Hexadecimal NUL + +echo "\n-- A longer and heredoc string --\n"; +$string = <<<EOD +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +EOD; +var_dump( strstr($string, "abcd") ); +var_dump( strstr($string, "1234") ); + +echo "\n-- A heredoc null string --\n"; +$str = <<<EOD +EOD; +var_dump( strstr($str, "\0") ); +var_dump( strstr($str, NULL) ); +var_dump( strstr($str, "0") ); + + +echo "\n-- simple and complex syntax strings --\n"; +$needle = 'world'; + +/* Simple syntax */ +var_dump( strstr("Hello, world", "$needle") ); // works +var_dump( strstr("Hello, world'S", "$needle'S") ); // works +var_dump( strstr("Hello, worldS", "$needleS") ); // won't work + +/* String with curly braces, complex syntax */ +var_dump( strstr("Hello, worldS", "${needle}S") ); // works +var_dump( strstr("Hello, worldS", "{$needle}S") ); // works + + +echo "\n-- complex strings containing other than 7-bit chars --\n"; +$str = chr(0).chr(128).chr(129).chr(234).chr(235).chr(254).chr(255); +echo "- Positions of some chars in the string '$str' are as follows -\n"; +echo chr(128)." => "; +var_dump( strstr($str, chr(128)) ); +echo chr(255)." => "; +var_dump( strstr($str, chr(255)) ); +echo chr(256)." => "; +var_dump( strstr($str, chr(256)) ); + +echo "\n*** Testing error conditions ***"; +var_dump( strstr($string, "")); +var_dump( strstr() ); // zero argument +var_dump( strstr("") ); // null argument +var_dump( strstr($string) ); // without "needle" +var_dump( strstr("a", "b", "c") ); // args > expected +var_dump( strstr(NULL, "") ); + +/* Cleaning the resources */ +fclose($resource1); +closedir($resource2); + +echo "\nDone"; ?> ---EXPECT-- +--EXPECTF-- +*** Testing basic functionality of strstr() *** string(11) "test string" string(6) "string" string(6) "string" @@ -29,21 +187,228 @@ bool(false) bool(false) bool(false) -string(32) "6ec19f52f0766c463f3bb240f4396913" -string(7) " string" -string(4) "test" ---UEXPECT-- -unicode(11) "test string" -unicode(6) "string" -unicode(6) "string" -unicode(8) "t string" -unicode(1) "g" -unicode(32) "7272696018bdeb2c9a3f8d01fc2a9273" + +*** Testing strstr() with various needles *** +-- Iteration 0 -- +string(86) "Hello world,012033 -3.3445 NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 1 -- bool(false) + +-- Iteration 2 -- +string(40) "+ +abcd$:Hello world" + +-- Iteration 3 -- +string(40) "+ +abcd$:Hello world" + +-- Iteration 4 -- +string(28) "+ +abcd$:Hello world" + +-- Iteration 5 -- +string(38) "abcd\xxyz + +abcd$:Hello world" + +-- Iteration 6 -- +string(32) "xyz + +abcd$:Hello world" + +-- Iteration 7 -- +string(25) "octal + +abcd$:Hello world" + +-- Iteration 8 -- +string(67) "-3.3445 NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 9 -- bool(false) + +-- Iteration 10 -- +string(67) "-3.3445 NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 11 -- bool(false) + +-- Iteration 12 -- +string(40) "+ +abcd$:Hello world" + +-- Iteration 13 -- +string(55) "NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 14 -- +string(74) "012033 -3.3445 NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 15 -- +string(40) "+ +abcd$:Hello world" + +-- Iteration 16 -- bool(false) + +-- Iteration 17 -- +string(50) "TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 18 -- +string(73) "12033 -3.3445 NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 19 -- bool(false) -unicode(32) "6ec19f52f0766c463f3bb240f4396913" -unicode(7) " string" -unicode(4) "test" + +-- Iteration 20 -- +string(40) "+ +abcd$:Hello world" + +-- Iteration 21 -- +string(45) "FALSE+ +abcd$:Hello world" + +-- Iteration 22 -- +string(81) " world,012033 -3.3445 NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 23 -- +string(60) " NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 24 -- +string(37) "bcd\xxyz + +abcd$:Hello world" + +-- Iteration 25 -- +bool(false) + +-- Iteration 26 -- +string(20) " + +abcd$:Hello world" + +-- Iteration 27 -- +string(73) "12033 -3.3445 NULL TRUE FALSE+ +abcd$:Hello world" + +-- Iteration 28 -- +bool(false) + +-- Iteration 29 -- +string(86) "Hello world,012033 -3.3445 NULL TRUE FALSE+ +abcd$:Hello world" + +*** Testing Miscelleneous input data *** +-- Passing objects as string and needle -- +string(5) "world" + +-- passing an array as string and needle -- + +Warning: strstr() expects parameter 1 to be string (Unicode or binary), array given in %s on line %d +NULL +string(27) "?world,!$%**()%**[][[[&@#~!" +string(20) "!$%**()%**[][[[&@#~!" + +-- passing Resources as string and needle -- + +Warning: strstr() expects parameter 1 to be string (Unicode or binary), resource given in %s on line %d +NULL + +Warning: strstr() expects parameter 1 to be string (Unicode or binary), resource given in %s on line %d +NULL + +-- Posiibilities with null -- +bool(false) +bool(false) +bool(false) +string(1) "0" + +-- A longer and heredoc string -- +string(729) "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789" +string(702) "123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789 +abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789" + +-- A heredoc null string -- +bool(false) +bool(false) +bool(false) + +-- simple and complex syntax strings -- +string(5) "world" +string(7) "world'S" + +Notice: Undefined variable: needleS in %s on line %d + +Warning: strstr(): Empty delimiter in %s on line %d +bool(false) +string(6) "worldS" +string(6) "worldS" + +-- complex strings containing other than 7-bit chars -- +- Positions of some chars in the string '+ => string(6) "êëþÿ" +ÿ => string(1) "ÿ" ++ +*** Testing error conditions *** +Warning: strstr(): Empty delimiter in %s on line %d +bool(false) + +Warning: strstr() expects at least 2 parameters, 0 given in %s on line %d +NULL + +Warning: strstr() expects at least 2 parameters, 1 given in %s on line %d +NULL + +Warning: strstr() expects at least 2 parameters, 1 given in %s on line %d +NULL +bool(false) + +Warning: strstr(): Empty delimiter in %s on line %d +bool(false) + +Done