2010-11-12 49 views
7

cách xác thực số thập phân trong PHP. Tôi nhìn is_numeric() nhưng điều đó sẽ không làm việc cho tôi:cách xác thực số thập phân trong PHP

bool is_numeric (var hỗn hợp)

Finds liệu biến nhất định là số. Các chuỗi số bao gồm dấu tùy chọn, bất kỳ số nào, phần thập phân tùy chọn và tùy chọn phần mũ. Do đó, + 0123.45e6 là giá trị số hợp lệ. Hệ thập lục phân ký hiệu (0xFF) được cho phép quá nhưng chỉ không có dấu, thập phân và phần mũ.

Tôi không muốn phần số mũ hoặc ký hiệu thập lục phân. Người dùng sẽ nhập các giá trị thập phân đơn giản và tôi không muốn loại-o chỉ xảy ra là số mũ hợp lệ hoặc giá trị hex để trượt qua. Tôi chỉ muốn số thập phân "truyền thống" để được hợp thức hóa.

EDIT đây là trang đơn giản (sức mạnh vũ phu) chứa dữ liệu kiểm tra hoàn chỉnh hơn nhiều (những gì nên và không nên được coi là giá trị số).

<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='[email protected]';  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> 
+0

Chỉ cần cho các đối số sake, bạn không nên được khoan dung về một cái gì đó như '--1' hoặc' 1' ++? –

+0

@ Jason McCreary, dữ liệu được sử dụng bởi một cái gì đó không khoan dung trên một cái gì đó như '--1' hoặc' ++ 1' – xyz

+0

Nên '.5' được coi là hợp lệ (như' 0,5')? – Hammerite

Trả lời

6

cập nhật với dữ liệu thử nghiệm của bạn.

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

$valid = TestFunction($input); 

Hoặc cắt đầu vào đầu tiên

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

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

Công trình này tuyệt vời !!!! – xyz

1
$decimal = preg_match('/^[+\-]?\d+(\.\d+)?$/', $value) ? (float)$value : 0.0; 
+0

Điều đó sẽ không khớp với các giá trị chứa một chữ số duy nhất, tôi tin/^ [+ \ -]? \ d + (?: \. \ d +)? $/có thể hoạt động –

+0

@rr: Nó khớp với các chữ số đơn. Tôi không phải là một chuyên gia regex, vì vậy tôi cắm nó vào một tập tin; nhưng theo như tôi biết, khả năng số duy nhất là những gì '()?' xung quanh '\. \ d +' là cho. – pinkgothic

+0

Dấu ngoặc đơn không có ở đó khi tôi đưa ra nhận xét đầu tiên :) –

0
$number = preg_match($number, '/\\A\\s*[+\\-]?\\d*(\\.\\d+)?\\s*\\Z/') ? 
      trim($number) : 
      'INVALID'; 
if ($number === '') { $number = '0'; } 
if ($number === 'INVALID') { 
    // handle 
} 
0

gì về biểu thức chính quy? preg_match(/[+\-]?\d*(\.\d+)?([eE][+\-]?d+)?/,$var); Các bit [eE] và sau khi nó cho phép mọi người nhập 2.3e2 cho số thực sự lớn hoặc thực sự nhỏ, vì vậy hãy để nó ra nếu bạn không muốn. Toàn bộ điều này sẽ cho phép:

2 
0.3 
.3 
2.2 
+2.2 
-2.2 
2.3e3 
2.3E-3 
0

Các biểu hiện thường xuyên sẽ giống như

[[email protected] ~]$ cat test.php 
<?php 

$regex = "/^([+-]{1})?[0-9]+(\.[0-9]+)?$/"; 

$numbers = array("1", "12345.1", "6789.01", "-1.1", "+1.1", "0", "00001.1"); 

foreach($numbers as $number) { 

     print preg_match($regex, $number)."\n"; 

} 
[[email protected] ~]$ php -e test.php 
1 
1 
1 
1 
1 
1 
1 
+0

Tôi quên bao gồm điều này trong các giá trị mẫu của tôi, nhưng '.5' không hoạt động với mã này. – xyz

0
$value = (float)$value; 
Các vấn đề liên quan