加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何在PHP中验证十进制数

发布时间:2020-12-13 17:55:30 所属栏目:PHP教程 来源:网络整理
导读:如何在 PHP中验证十进制数.我查看了is_numeric(),但这对我不起作用: bool is_numeric ( mixed var ) Finds whether the given variable is numeric. Numeric strings consist of optional sign,any number of digits, optional decimal part and optional e
如何在 PHP中验证十进制数.我查看了is_numeric(),但这对我不起作用:

bool is_numeric ( mixed var )

Finds whether the given variable is
numeric. Numeric strings consist of
optional sign,any number of digits,
optional decimal part and optional
exponential part
. Thus +0123.45e6 is a
valid numeric value. Hexadecimal
notation (0xFF) is allowed too
but
only without sign,decimal and
exponential part.

我不想要指数部分或十六进制表示法.用户将输入简单的十进制值,我不希望类型-o碰巧是有效的指数或十六进制值.我只想将“传统”十进制数字视为有效.

在这里编辑一个简单(强力)页面,其中包含更完整的测试数据(应该和不应该被视为数值).

<html><head></head>
<body>

<?php

function TestFunction($s_value) {
    //
    //  your code here
    //
    return; //true or false;
}

print '<b>these are valid numbers and should return "true"</b><br>';
print '<pre>';
    $s_value='123';     print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='+1';      print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='-1';      print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='  1';     print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1  ';     print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='  1  ';   print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1';       print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='12345.1'; print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='6789.01'; print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='-1.1';    print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='+1.1';    print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='0';       print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='00001.1'; print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='.1';      print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='.0000001';print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='5.';      print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
print '</pre>';

print '<br><b>these are NOT valid numbers and should return "false"</b><br>';
print '<pre>';

    $s_value='--------------------------------';print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value=null;      print "n".'$s_value=null,TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='.';       print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='';        print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value=' ';       print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='  ';      print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1abc';    print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='$1';      print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1@';      print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1.2.1';   print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='abc';     print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1.45e6';  print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='0xFF';    print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='++1';     print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='--1';     print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1+';      print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1-';      print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='a1';      print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='#1';      print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='10.e5';   print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='0x1';     print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='0x';      print "n".'$s_value="'.$s_value.'",TestFunction()='.(TestFunction($s_value)?'true':'false');
print '</pre>';

?>

</body>
</html>
更新了您的测试数据.
function TestFunction($s_value) {
    $regex = '/^s*[+-]?(?:d+(?:.d*)?|.d+)s*$/';
    return preg_match($regex,$s_value); 
}

$valid = TestFunction($input);

或者首先修剪输入

function TestFunction($s_value) {
    $regex = '/^[+-]?(?:d+(?:.d*)?|.d+)$/';
    return preg_match($regex,$s_value); 
}

$input = trim($input);
$valid = TestFunction($input);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读