xref: /PHP-7.1/ext/standard/tests/strings/trim1.phpt (revision 113213f0)
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--
63string(0) ""
64string(0) ""
65string(1) "0"
66string(1) "0"
67string(13) "	testing trim"
68string(17) "  	testing trim  "
69string(15) "	testing trim  "
70
71*** Testing error conditions ***
72
73Warning: trim() expects at least 1 parameter, 0 given in %s on line %d
74NULL
75
76Warning: trim() expects at most 2 parameters, 3 given in %s on line %d
77NULL
78
79Warning: trim() expects at most 2 parameters, 3 given in %s on line %d
80NULL
81
82*** Testing with OBJECTS ***
83string(4) "bjec"
84
85*** Testing with String with embedded NULL ***
86string(22) "234�05678�00efgh\xijkl"
87
88*** Testing with heredoc string ***
89string(12) " heredoc str"
90
91Done
92