PHP - 如何询问字段是包含数字还是+或空格还是空?

My website form is getting hammered with spam. I have noticed in the "Phone" field the spam bots always insert text rather that a number so I would like to add an if statement to the php mailer blocking the email if the phone field doesn't contain any of the following:

1) I want users to be able to leave the field blank, so empty field must be accepted.

2) Must contain "numbers" or "plus sign" or "spaces"

How would I write this in PHP?

Any help is appreciated

EDIT: Just though lol it would be much easier to just check if the field contains alphabetical characters. How would I do this?

EDIT2: Sorted. I used "if (ctype_alpha ($phone) !== false)"

Regular expressions are probably the best way, although not necessarily the easiest to understand at first. But regular expressions are definitely a good thing to learn if you are not familiar with them. My favorite introduction is this site: http://www.zytrax.com/tech/web/regex.htm And this is a good site for interactively building a regex and seeing how it works in realtime: http://www.regexr.com/ I'm sure there are plenty of other similar sites but those are the two I always go back to.

If you search around for a regular expression solution you will find countless possibilities and variations. My personal advice is to keep it simple. I would start with considering how you store the phone number data. I usually just keep the numbers, so I would simplify it by first removing those "allowed" characters and then checking if what's left over is just numbers.

$phone = str_replace(Array('+', ' ', '(', ')'), '', $phone);

That will replace all pluses, spaces, and parentheses with an empty string (i.e. remove them). Then you can check if the string is numeric, and if it is store it, otherwise print/return an error.

if (!is_numeric($phone))
    // stop processing and output an error

First of all You must use some spamblock for example: token, honey pot, captcha etc.

In my country mobile or local phone number contains only 9digits without country code which is +XX. So i create INT(10) field in db. After submit form remove everything without digits.

For example:

$phoneNumber = (int) substr( preg_replace( '#[^\d]+#', '', $_POST['phone_numer'] ), 0, 9 );

In many project allways works.