/hphp/test/zend/good/ext/standard/tests/strings/substr.php
PHP | 78 lines | 41 code | 19 blank | 18 comment | 7 complexity | 228191b2da86b3bb88a7afb9ff9a82cc MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
- <?hh
- /* Prototype: string substr( string str, int start[, int length] )
- * Description: Returns the portion of string specified by the start and length parameters.
- */
- <<__EntryPoint>> function main(): void {
- $strings_array = varray[ "", '12345', "abcdef", "123abc", "_123abc"];
- /* Testing for error conditions */
- echo "*** Testing for error conditions ***\n";
- /* Zero Argument */
- try { var_dump( substr() ); } catch (Exception $e) { echo "\n".'Warning: '.$e->getMessage().' in '.__FILE__.' on line '.__LINE__."\n"; }
- /* NULL as Argument */
- try { var_dump( substr(NULL) ); } catch (Exception $e) { echo "\n".'Warning: '.$e->getMessage().' in '.__FILE__.' on line '.__LINE__."\n"; }
- /* Single Argument */
- try { var_dump( substr("abcde") ); } catch (Exception $e) { echo "\n".'Warning: '.$e->getMessage().' in '.__FILE__.' on line '.__LINE__."\n"; }
- /* Scalar Argument */
- try { var_dump( substr(12345) ); } catch (Exception $e) { echo "\n".'Warning: '.$e->getMessage().' in '.__FILE__.' on line '.__LINE__."\n"; }
- /* more than valid number of arguments ( valid are 2 or 3 ) */
- try { var_dump( substr("abcde", 2, 3, 4) ); } catch (Exception $e) { echo "\n".'Warning: '.$e->getMessage().' in '.__FILE__.' on line '.__LINE__."\n"; }
- $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) );
- /* start <0 && -start > length */
- echo "\n*** Start before the first char ***\n";
- var_dump (substr("abcd" , -8) );
- echo"\nDone";
- }