is_numeric[]
kiểm tra xem một giá trị có phải là một số hay không. Nó không nhất thiết phải là một số nguyên mặc dù - nó có thể là một số thập phân hoặc một số trong ký hiệu khoa học.
Ví dụ preg_match[]
mà bạn chỉ đưa ra chỉ kiểm tra xem giá trị có chứa các chữ số từ 0 đến chín; Bất kỳ số lượng trong số họ, và trong bất kỳ chuỗi.
Lưu ý rằng biểu thức thông thường mà bạn đã đưa ra cũng không phải là trình kiểm tra số nguyên hoàn hảo, cách bạn đã viết nó. Nó không cho phép tiêu cực; Nó cho phép một chuỗi có độ dài bằng không [nghĩa là không có chữ số nào, có lẽ không nên hợp lệ?], Và nó cho phép số có số 0 không có số 0, một lần nữa có thể không được dự định.
[EDIT]
Theo bình luận của bạn, một biểu thức thông thường tốt hơn có thể trông như thế này:
/^[1-9][0-9]*$/
Điều này buộc chữ số đầu tiên chỉ là từ 1 đến 9, vì vậy bạn không thể có số không hàng đầu. Nó cũng buộc nó phải dài ít nhất một chữ số, do đó giải quyết vấn đề chuỗi có độ dài bằng không.
Bạn không lo lắng về những tiêu cực, vì vậy đó không phải là vấn đề.
Bạn có thể muốn hạn chế số lượng các chữ số, bởi vì mọi thứ đứng, nó sẽ cho phép các chuỗi quá lớn để được lưu trữ dưới dạng số nguyên. Để hạn chế điều này, bạn sẽ thay đổi ngôi sao thành hạn chế độ dài như vậy:
/^[1-9][0-9]{0,15}$/
Điều này sẽ cho phép chuỗi dài từ 1 đến 16 chữ số [tức là chữ số đầu tiên cộng với 0-15 chữ số nữa]. Hãy thoải mái điều chỉnh các con số trong niềng răng xoăn phù hợp với nhu cầu của riêng bạn. Nếu bạn muốn một chuỗi độ dài cố định, thì bạn chỉ cần chỉ định một số trong niềng răng.
Hy vọng điều đó sẽ giúp.
[Php 4, Php 5, Php 7, Php 8]
is_numeric - tìm thấy một biến là số hay chuỗi số — Finds whether a variable is a number or a numeric string
Sự mô tả
is_numeric [hỗn hợp $value
]: bool[mixed $value
]:
bool
Thông số
________số 8Biến được đánh giá.
Trả về giá trị
Trả về true
nếu value
là một số hoặc chuỗi số,
/^[1-9][0-9]{0,15}$/
1 nếu không.true
if value
is a number or a numeric string, /^[1-9][0-9]{0,15}$/
1 otherwise. Thay đổi
8.0.0 | is_numeric [hỗn hợp $value ]: booltrue . Previously, 1 was return instead.
|
Thông số
________số 8is_numeric[] examples
/^[1-9][0-9]{0,15}$/
5/^[1-9][0-9]{0,15}$/
6/^[1-9][0-9]{0,15}$/
7Biến được đánh giá.
'42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array [ ] is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric
Trả về giá trịis_numeric[] with whitespace
/^[1-9][0-9]{0,15}$/
8/^[1-9][0-9]{0,15}$/
6/^[1-9][0-9]{0,15}$/
7 Trả về true
nếu value
là một số hoặc chuỗi số,
/^[1-9][0-9]{0,15}$/
1 nếu không.' 42' is numeric '42 ' is numeric ' 9001' is NOT numeric '9001 ' is NOT numeric
Thay đổi
' 42' is numeric '42 ' is NOT numeric ' 9001' is NOT numeric '9001 ' is NOT numeric
Phiên bản
- Các chuỗi số kết thúc bằng khoảng trắng [
2] bây giờ sẽ trả về/^[1-9][0-9]{0,15}$/
true
. Trước đây,
1 đã trở lại thay thế./^[1-9][0-9]{0,15}$/
- Ví dụ
- Ví dụ #1 is_numeric [] ví dụ
- Ví dụ trên sẽ xuất ra:
- Ví dụ #2 is_numeric [] với khoảng trắng
- Đầu ra của ví dụ trên trong Php 8:
- Đầu ra của ví dụ trên trong Php 7:
- Xem thêm
- Chuỗi số
- ctype_digit [] - Kiểm tra [các] ký tự số
is_bool [] - Tìm hiểu xem một biến có phải là boolean ¶
is_null [] - tìm thấy một biến có phải là null
'42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array [ ] is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric1
'42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array [ ] is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric2
'42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array [ ] is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric3
'42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array [ ] is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric4
'42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array [ ] is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric5
'42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array [ ] is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric6
'42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array [ ] is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric7
is_float [] - Tìm liệu loại biến có nổi không ¶
is_int [] - Tìm loại biến có số nguyên không
'42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array [ ] is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric8
is_String [] - Tìm loại biến là chuỗi ¶
is_Object [] - Tìm xem một biến có phải là một đối tượng
'42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array [ ] is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric9
' 42' is numeric '42 ' is numeric ' 9001' is NOT numeric '9001 ' is NOT numeric0
' 42' is numeric '42 ' is numeric ' 9001' is NOT numeric '9001 ' is NOT numeric1
'42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array [ ] is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric7
is_array [] - Tìm xem một biến có phải là một mảng không ¶
Filter_var [] - Lọc một biến có bộ lọc được chỉ định
' 42' is numeric '42 ' is numeric ' 9001' is NOT numeric '9001 ' is NOT numeric3
' 42' is numeric '42 ' is numeric ' 9001' is NOT numeric '9001 ' is NOT numeric4
' 42' is numeric '42 ' is numeric ' 9001' is NOT numeric '9001 ' is NOT numeric5
' 42' is numeric '42 ' is numeric ' 9001' is NOT numeric '9001 ' is NOT numeric6
' 42' is numeric '42 ' is numeric ' 9001' is NOT numeric '9001 ' is NOT numeric7
'42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array [ ] is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric7
Sobolanx tại gmail dot com ¶ ¶
11 năm trước
' 42' is numeric '42 ' is numeric ' 9001' is NOT numeric '9001 ' is NOT numeric9
' 42' is numeric '42 ' is NOT numeric ' 9001' is NOT numeric '9001 ' is NOT numeric0
' 42' is numeric '42 ' is NOT numeric ' 9001' is NOT numeric '9001 ' is NOT numeric1
'42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array [ ] is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric7