-4

I got a function to validate if a character is white or not. But it is not working. It always returns false. What I am doing wrong?

function is_white($char)
{
$white_list = array(9,10,13,32);
return in_array($char,$white_list);
}
var_dump(is_white("\n"));

Thank you for you help.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80

4 Answers4

2

You're trying to compare a string with a list of integers.

You could just use trim() to check if the string is empty.

function is_white($char) {
    return !trim($char) ;
}
var_dump(is_white("\n")) ; // true

But if you want to compare characters you have to use ord() that convert a character to its integer equivalent.

function is_white($char)
{
    $white_list = array(9,10,13,32);
    return in_array(ord($char),$white_list);
}
Syscall
  • 19,327
  • 10
  • 37
  • 52
  • Using `trim()` is what I would do too, however you might want to include a check to make sure the length of the original string is 1 to make sure it's a single character. – Mike Feb 15 '18 at 00:01
2

You could use a built-in function for this.

var_dump(ctype_space("\n"));

It takes a string of characters, but of course it will still work with a single character.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • +1 I honestly can't believe a function like that even exists. The use case seems to me to be way too specific. – Mike Feb 15 '18 at 01:47
  • This does not fix OP's code ("*What I am doing wrong?*") – tevemadar Feb 15 '18 at 11:29
  • @tevemadar That's true, but it doesn't make this answer invalid, if that's what you're implying. Showing an alternate way to accomplish the stated goal is always a valid answer, and often a more useful answer than just fixing the OP's code. Based on the text in the question, other people who find it in the future won't be looking for `ord`, and others had already answered "What am I doing wrong?" so it wouldn't be useful for me to restate it for the OP's benefit. I would have voted to close rather than answering if I knew that duplicate existed, and the dup isn't related to `ord` either. – Don't Panic Feb 15 '18 at 16:46
1

You need the ASCII integer value of the character, before checking if it's in the white list array:

You can use ord for it:

function is_white($char)
{
    $white_list = array(9,10,13,32);
    return in_array(ord($char),$white_list);
}
var_dump(is_white("\n"));

More information: PHP Function ORD

1

If you want to handle ALL possible white spaces including unicode:

echo preg_match('~^\p{Z}$~u',$char)?'white':'non-white';

This will cover these entities:

$chars=[
    "\u0009", // CHARACTER TABULATION (\t)
    "\u000A", // LINE FEED (LF) (\n)
    "\u000B", // LINE TABULATION
    "\u000C", // FORM FEED (FF)
    "\u000D", // CARRIAGE RETURN (CR) (\r)
    "\u0020", // SPACE ( )
    "\u0085", // NEXT LINE (NEL) 
    "\u00A0", // NO-BREAK SPACE
    "\u1680", // OGHAM SPACE MARK
    "\u180E", // MONGOLIAN VOWEL SEPARATOR
    "\u2000", // EN QUAD 
    "\u2001", // EM QUAD 
    "\u2002", // EN SPACE
    "\u2003", // EM SPACE
    "\u2004", // THREE-PER-EM SPACE
    "\u2005", // FOUR-PER-EM SPACE
    "\u2006", // SIX-PER-EM SPACE
    "\u2007", // FIGURE SPACE
    "\u2008", // PUNCTUATION SPACE
    "\u2009", // THIN SPACE
    "\u200A", // HAIR SPACE
    "\u2028", // LINE SEPARATOR
    "\u2029", // PARAGRAPH SEPARATOR
    "\u202F", // NARROW NO-BREAK SPACE
    "\u205F", // MEDIUM MATHEMATICAL SPACE
    "\u3000"  // IDEOGRAPHIC SPACE 
];
mickmackusa
  • 43,625
  • 12
  • 83
  • 136