FlyingUnderpants FlyingUnderpants - 1 year ago 76
PHP Question

preg_match check for number and whitespaces

I'm working on a simple login & register system as practice and I want to check strings if they contain a number and at te same time with preg_match, but how can I make that happen?

if (preg_match('~[0-9]~', $name)) { echo "match";}

This is the code I have so far. It only can find a number inside a string, but how can I add that the preg_match also searches for whitespaces. I have tried multiple things but I just can't find a way to make it work. I hope someone can help me out.

If there is something unclear please let me know, and I will do my best to help you.

Thanks in advance :D

Dez Dez
Answer Source

If you want to find white spaces you have to use \s in the regular expression.

preg_match('~[0-9\s]~', $name)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download