intval() question
Posted: Thu Dec 22, 2011 6:41 am
Hello,
Basically what I'm trying to do is validate a numeric input. This will be used in a function to validate d.o.b from the form later. To shield the application from arbitrary input I need to make sure the number supplied in dob is actually a number (Problem is, I want to allow both . and / dividers for the date so that both 01.01.2011 and 01/01/2011 are both valid). I read about intval() which, as far as I understand, is supposed to return the numeric value of the string (that is only numbers but not letters or symbols). So the following is just a function which is supposed to perform the task:
And this is the test code:
And finally the output:
Test
12
0
which is not what I want. I'm starting to think I misinterpreted the intval() function. Is there another function that removes all non numeric characters from the string or is that something I have to write myself, or something I have done wrong with intval? It's not hard to write a string numeric filter but as far as I know most of the standard functions are in C, which is faster than a function in php.
Thanks in advance!
Basically what I'm trying to do is validate a numeric input. This will be used in a function to validate d.o.b from the form later. To shield the application from arbitrary input I need to make sure the number supplied in dob is actually a number (Problem is, I want to allow both . and / dividers for the date so that both 01.01.2011 and 01/01/2011 are both valid). I read about intval() which, as far as I understand, is supposed to return the numeric value of the string (that is only numbers but not letters or symbols). So the following is just a function which is supposed to perform the task:
Code: Select all
function numericFilter($input){
return intval($input);
}Code: Select all
<?php
$testString = "12rrr44";
$testString2 = "a12rrr44";
echo numericFilter($testString)."<br>";
echo numericFilter($testString2)."<br>";
?>Test
12
0
which is not what I want. I'm starting to think I misinterpreted the intval() function. Is there another function that removes all non numeric characters from the string or is that something I have to write myself, or something I have done wrong with intval? It's not hard to write a string numeric filter but as far as I know most of the standard functions are in C, which is faster than a function in php.
Thanks in advance!