xref: /PHP-5.5/ext/standard/tests/strings/trim1.phpt (revision 02e4d7a2)
1--TEST--
2Test trim() function
3--FILE--
4<?php
5
6/* Prototype: string trim( string str [,string charlist] )
7 * Strip whitespace (or other characters) from the beginning and end of a string.
8 */
9
10/* trim with unset/null/boolean variable - retuns an empty string */
11echo "\n";
12$null_var = NULL;
13var_dump( trim($null_var) );
14$null_var = "";
15var_dump( trim($null_var) );
16$null_var = 0;
17var_dump( trim($null_var) );
18$bool_val = true;
19var_dump( trim($null_var) );
20
21/* second argument charlist as null - does not trim any white spaces */
22var_dump( trim("\ttesting trim", "") );
23var_dump( trim("  \ttesting trim  ", NULL) );
24var_dump( trim("\ttesting trim  ", true) );
25
26/* Testing error conditions */
27echo "\n*** Testing error conditions ***\n";
28
29//Zero arguments
30var_dump( trim() );
31// More than expected number of args */
32var_dump( trim("\tstring\n", "\t\n", $null_var) );
33var_dump( trim(NULL, "", NULL ) );
34
35
36/* Use of class and objects */
37echo "\n*** Testing with OBJECTS ***\n";
38class string1
39{
40  public function __toString() {
41    return "Object";
42  }
43}
44$obj = new string1;
45var_dump( trim($obj, "Ot") );
46
47/* String with embedded NULL */
48echo "\n*** Testing with String with embedded NULL ***\n";
49var_dump( trim("\x0n1234\x0005678\x0000efgh\xijkl\x0n1", "\x0n1") );
50
51/* heredoc string */
52$str = <<<EOD
53us
54ing heredoc string
55EOD;
56
57echo "\n*** Testing with heredoc string ***\n";
58var_dump( trim($str, "us\ning") );
59
60echo "\nDone";
61?>
62--EXPECTF--
63
64string(0) ""
65string(0) ""
66string(1) "0"
67string(1) "0"
68string(13) "	testing trim"
69string(17) "  	testing trim  "
70string(15) "	testing trim  "
71
72*** Testing error conditions ***
73
74Warning: trim() expects at least 1 parameter, 0 given in %s on line %d
75NULL
76
77Warning: trim() expects at most 2 parameters, 3 given in %s on line %d
78NULL
79
80Warning: trim() expects at most 2 parameters, 3 given in %s on line %d
81NULL
82
83*** Testing with OBJECTS ***
84string(4) "bjec"
85
86*** Testing with String with embedded NULL ***
87string(22) "234�05678�00efgh\xijkl"
88
89*** Testing with heredoc string ***
90string(12) " heredoc str"
91
92Done
93