Check if string contains no characters except numbers?
I need to check if a string contains NO other characters except numbers (0,1,2,3,4,5,6,7,8,9).
开发者_StackOverflow中文版I'm expecting the user to input a time (12:00). I explode()
the input to seperate the hours and minutes. I now need to make sure the time is an actual time value.
$time = explode(":",$time);
if ($time['0'] > 12 || $time['0'] < 1 || not_actual_numbers)
{
//error: not valid hour
echo("error: time val hour is bad");
}
You can use is_int
$time = explode(":",$time);
if ($time['0'] > 12 || $time['0'] < 1 || !is_int($time[0]))
{
//error: not valid hour
echo("error: time val hour is bad");
}
精彩评论