TomSawyer
TomSawyer

Reputation: 3820

PHP - Trim doesn't work with white space?

Today, i have very weird problem with trim function. Here is my code. =and result is 124. Trim function didn't work with this string ??!

$s = '                                                                                  ';
$s = trim($s);
echo strlen($s);exit();

Copy & paste doesn't work with this code. i don't know what white space is? but trim couldn't strip it :|

you can test this code here:

test: http:// 70.38.18.62/t.php

You can copy white space and code over there: http://jsbin.com/ukunuq/1/edit

Upvotes: 1

Views: 7445

Answers (3)

s.webbandit
s.webbandit

Reputation: 17000

Due to PHP DOCS trim(); function with no second paramenter escapes

" " (ASCII 32 (0x20)), an ordinary space.
"\t" (ASCII 9 (0x09)), a tab.
"\n" (ASCII 10 (0x0A)), a new line (line feed).
"\r" (ASCII 13 (0x0D)), a carriage return.
"\0" (ASCII 0 (0x00)), the NUL-byte.
"\x0B" (ASCII 11 (0x0B)), a vertical tab.

So yo have only one of this characters in $s variable. Others are not visible but are not one from the list.

Upvotes: 0

Robbie
Robbie

Reputation: 17710

When I look at your t.txt, the $s string is not just spaces. In Hex, it reads a mixture of 20 (space) and C2 and A0 characters. You need to lose those C2 and A0 characters.

They may not display on screen, but they are NOT spaces. Here's my sniff of what's returned

Screen Capture

Possibly encoding the file in 7-bit ASCII would also reveal the problem.

To clean it up, just select all the text in $s, delete it and then hit space bar to get the right number of spaces. Or use str_repeat(' ', 134) that will do the same. Check the charset of your editor.

Upvotes: 8

Marc B
Marc B

Reputation: 360572

Works here:

marc@panic:~$ php -a
Interactive shell

php > $s = '                                                                                  ';
php > $s = trim($s);
php > echo strlen($s);exit();
0
marc@panic:~$ 

PHP 5.3.10-1ubuntu3.2. Not sure where you're getting 124 from. strlen of the original untrimmed $s comes out as 82 for me.

Upvotes: 0

Related Questions