Reputation: 5773
I just want to know the method to check a PHP variable for any non-numbers and if it also detects spaces between characters? Need to make sure nothing weird gets put into my form fields. Thanks in advance.
Upvotes: 34
Views: 73533
Reputation: 18810
This worked out to be ~30% faster than preg_match
for my test cases, while still letting you match any characters you want:
if( $a !== '' && trim($a, ' 1234567890.,') === '' ){
print("The variable contains only digits, decimal separators and spaces");
}
This simply removes all characters supplied from the string. If the result is an empty string, you know it only contained those characters.
Upvotes: 3
Reputation: 39476
If you mean that you only want a value to contain digits then you can use ctype_digit()
.
Upvotes: 43
Reputation: 49713
You can use is_numeric()
:
if ( is_numeric($_POST['foo']) ) {
$foo = $_POST['foo'];
} else {
// Error
}
This will check that the value is numerical, so it may contain something else than digits:
12
-12
12.1
But this will ensure that the value is a valid number.
Upvotes: 33
Reputation: 4095
Assuming you want only (and only) valid integers and you dont want users to mess up with your data and database with hexadecimal
or binary
or any other form of numbers then you can always use this method:
if(((string) (int) $stringVariable) === $stringVariable) {
// Thats a valid integer :p
}
else {
// you are out of luck :(
}
The trick is simple. It casts the variable of type string to integer type and then casts it back to string. Super fast, Super simple.
For the sake of testing I've prepared a test:
'1337' is pure integer.
'0x539' is not.
'02471' is not.
'0000343' is not.
'0b10100111001' is not.
'1337e0' is not.
'not numeric' is not.
'not numeric 23' is not.
'9.1' is not.
'+655' is not.
'-586' is pure integer.
The only place that this method falls short is on negative numbers, so you need to check that beside this (use ((string) (int) $stringVariable) === $stringVariable && $stringVariable[0] !== "-"
).
Now I thought that the preg_match
method is the best. but in any project that deals with users, speed is an important factor. so I prepared a benchmarking test doing that above test for 500,000 times and the results were amazing:
My own invented method took only:
6.4700090885162
seconds to complete as compared to preg_match
which took:
77.020107984543
seconds to complete this test!
Upvotes: 1
Reputation: 471
This will check whether the input value is numeric or not. Hope this helps
if(!preg_match('#[^0-9]#',$value))
{
echo "Value is numeric";
}
else
{
echo "Value not numeric";
}
Upvotes: 4
Reputation: 48387
Cast and compare:
function string_contain_number($val)
{
return ($val + 0 == $val) ? true : false;
}
Upvotes: 1
Reputation: 417
PHP has the function is_numeric() which may be what you are looking for.
Upvotes: 1
Reputation: 6156
This will return true
if there are non-numbers in the string. It detects letters, spaces, tabs, new lines, whatever isn't numbers.
preg_match('#[^0-9]#',$variable)
Upvotes: 15
Reputation: 546503
You can use ctype_digit
eg:
if (!ctype_digit($myString)) {
echo "Contains non-numbers.";
}
Upvotes: 19