Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if there are only spaces in string in PHP?

Tags:

string

php

utf-8

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

like image 586
Bruce Dou Avatar asked Nov 18 '09 08:11

Bruce Dou


People also ask

How do you check if a string has only spaces?

To check if a string contains only spaces, call the trim() method on the string and check if the length of the result is equal to 0 . If the string has a length of 0 after calling the trim method, then the string contains only spaces.

How do you check if a string is empty in PHP?

We can use empty() function to check whether a string is empty or not. The function is used to check whether the string is empty or not. It will return true if the string is empty.

Is space a character in PHP?

ctype_space() function in PHPIt returns TRUE if every character in text creates some sort of white space, FALSE otherwise. Besides the blank character this also includes tab, vertical tab, line feed, carriage return and form feed characters.

What is whitespace in PHP?

A whitespace is any character that renders as a space, that is: A space character. A tab character. A carriage return character. A new line character.


2 Answers

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.

like image 130
Peter Lindqvist Avatar answered Oct 26 '22 15:10

Peter Lindqvist


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.

like image 41
pablasso Avatar answered Oct 26 '22 14:10

pablasso