7
print_r(strlen(trim('     ')));

the result is 9

I also tried

preg_replace('/[\n\r\t\s]/', '', '   ')

but the result is not zero.

Please download my code and you will get the result

http://blog.eood.cn/attachment.php?id=70

17
  • 3
    Your two attempts don't even make sense, nor do your results. Trimming that first string should result in an empty string (length 0). preg_replace is for replacing strings, not checking if they exist. Commented Nov 18, 2009 at 8:22
  • sorry for the error in title. Commented Nov 18, 2009 at 8:24
  • 2
    @Bruce: on my install : print_r(strlen(trim(' '))); = 0 wondering how you get 9! Commented Nov 18, 2009 at 8:24
  • echo strlen(trim(' ')); prints 0. Commented Nov 18, 2009 at 8:26
  • i copied the code from this page, the reuslt is zero. may be the space is different. But i don't know why my result is not zero of my code. Commented Nov 18, 2009 at 8:32

9 Answers 9

13
mb_language('uni');
mb_internal_encoding('UTF-8');

$s = '     ';
if (strlen(preg_replace('/\s+/u','',$s)) == 0) {
    echo "String is empty.\n";
}

If that doesn't work i suggest doing this

$s = '     ';
if (strlen(trim(preg_replace('/\xc2\xa0/',' ',$s))) == 0) {
    echo "String is empty.\n";
}

These solutions have been tested on different platforms.

The u flag tells preg_replace() to treat the string as a multibyte string, namely utf-8

The character is a nonbreaking space C2A0 and can be generated with alt+0160.

Sign up to request clarification or add additional context in comments.

4 Comments

Can you download my code and check the space, i think it is not normal space.
Note that empty also returns true for "0".
You may need to specify the internal encoding.
there was one non breaking space in your code, but the preg_replace would convert them all to regular space and then trim can take care of it.
3

Maybe you are doing something else that is messing up the results? Your test do returns 0

print_r(strlen(trim('     ')));

And that's the expected behavior of trim.

This function returns a string with whitespace stripped from the beginning and end of str . Without the second parameter, trim() will strip these characters:

  • " " (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.

UPDATE:

Looking at your attached code i noticed you have an extra character between 2 spaces.

This is the output of hexdump -C

$ hexdump -C  space.php 
00000000  3c 3f 0d 0a 70 72 69 6e  74 5f 72 28 73 74 72 6c  |<?..print_r(strl|
00000010  65 6e 28 74 72 69 6d 28  27 20 c2 a0 20 27 29 29  |en(trim(' .. '))|
00000020  29 3b 0d 0a 3f 3e                                 |);..?>|
00000026

And this is the output of od, with just that character in the file.

$ od space.php 
0000000    120302                                                        
0000002

trim won't delete that space, because.. well, it's not a space. This is a good reference on how to spot unusual characters.

Oh, and to answer your updated question, just use empty as Peter said.

Comments

2

I think the fastest way is to trim leading spaces (ltrim will fail fast if there are other characters) and compare the result to the empty string:

# Check if string consists of only spaces
if (ltrim($string, ' ') === '') {

Comments

2

A simple preg_match() would suffice:

if(preg_match('/^\s+$/', $str)) == 1){
 die('there are only spaces!');
}

Comments

0

You could use the count_chars function or the substr_count function.

Comments

0

You want to know if a string contains a space?

if(strpos($string, ' ') !== false) echo $string.' contains a space';

Comments

0

How about this...

$str = preg_replace('/\s\s+/', '', $str);

Or this...

$str = str_replace(array("\n", "\r", "\t", " ", "\o", "\xOB"), '', $str);

Comments

0
if(strlen(trim($_POST['foobar'])) == 0){
 die('the user didn\'t input anything!');
}

empty would also make it

like

$bar = trim($_POST['foobar']);
if(empty($bar)){
 die('the user didn\'t input anything!');
}

3 Comments

I cannot get the empty statement to run, what's the deal with that?
Ah, you have to assign the trimmed string to a variable before using empty.
Beware to empty(): empty('0') returns True as well!
0

if trim($var) is not working then $var may be not a string. so first cast into string

$var1 = string($var) and then trim($var1).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.