因此,如果用户键入1,000.00,则这是有效数字。如果用户输入1,0,000,00.0000,0,该怎么办? 你看到这里有多个错误,你的值不能是1,0 ...或000,00,因为它必须至少是逗号后面的3个数字。为了进一步扩展它,在数字中的点后面你不能有逗号。
$ keyword = $ _POST [' keyword'];
if (preg_match('/0/',$keyword) ||preg_match('/1/',$keyword) ||preg_match('/2/',$keyword)||preg_match('/3/',$keyword) ||preg_match('/4/',$keyword) || preg_match('/5/',$keyword) || preg_match('/6/',$keyword) || preg_match('/7/',$keyword) ||preg_match('/8/',$keyword) ||preg_match('/9/',$keyword) ||preg_match('/-/',$keyword) ||preg_match('/+/',$keyword) || preg_match('/,/',$keyword) ) {
$keywordtest = $keyword;
if ($keyword[0] == '+' || $keyword[0] == '-')
{
$keywordtest = substr_replace($keyword, '1', 0, 1);
echo $keywordtest;
}
if (preg_match('/,/',$keyword))
{
$x = 0;
while(true)
{
$findme = ',';
$pos = strpos($keyword, $findme);
if ($pos !== false)
{
$posArray[x] = $pos;
$x = x + 1;
if (x == 10)
break;
}
}
$numberCheck = posArray[x-1];
if (is_numeric($numberCheck))
{
}
else
{
echo "false '{$keyword}' is not numeric", PHP_EOL;
return 0;
}
$numberCheck = posArray[x+1];
if (is_numeric($numberCheck))
{
}
else
{
echo "false '{$keyword}' is not numeric", PHP_EOL;
return 0;
}
$numberCheck = posArray[x+2];
if (is_numeric($numberCheck))
{
}
else
{
echo "false '{$keyword}' is not numeric", PHP_EOL;
return 0;
}
$numberCheck = posArray[x+3];
if (is_numeric($numberCheck))
{
}
else
{
echo "false '{$keyword}' is not numeric", PHP_EOL;
return 0;
}
}
test_numeric($keywordtest,$keyword);
return 0;
}
else
{
echo "false '{$keyword}' is not numeric", PHP_EOL;
}
function test_numeric($keywordtest,$keyword)
{
if (is_numeric($keywordtest)){
echo "true '{$keyword}' is numeric", PHP_EOL;
}
return 0;
}
>
答案 0 :(得分:2)
您需要将filter_var与FILTER_VALIDATE_FLOAT filter type和FILTER_FLAG_ALLOW_THOUSAND标志结合使用。
然后它真的很简单:
// $val == 100000
$val = filter_var('100,000.00', FILTER_VALIDATE_FLOAT, array('flags' => FILTER_FLAG_ALLOW_THOUSAND));
// $val == -100000
$val = filter_var('-100,000.00', FILTER_VALIDATE_FLOAT, array('flags' => FILTER_FLAG_ALLOW_THOUSAND));
// $val === false (invalid)
$val = filter_var('100,000.00,00', FILTER_VALIDATE_FLOAT, array('flags' => FILTER_FLAG_ALLOW_THOUSAND));