如何在 PHP 中获取整数的长度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3998482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get length of integers in PHP ?
提问by GitsD
I want to get the length of integer values for validation in PHP. Example: Mobile numbers should be only 10 integer values. It should not be more than 10 or less than 10 and also it should not be included of alphabetic characters.
我想在 PHP 中获取用于验证的整数值的长度。示例:手机号码应仅为 10 个整数值。它不应超过 10 个或少于 10 个,并且不应包含字母字符。
How can I validate this?
我怎样才能验证这一点?
采纳答案by Alex Howansky
if (preg_match('/^\d{10}$/', $string)) {
// pass
} else {
// fail
}
回答by mdm
$num_length = strlen((string)$num);
if($num_length == 10) {
// Pass
} else {
// Fail
}
回答by R?mulo M. Farias
This will work for almost all cases (except zero) and easily coded in other languages:
这几乎适用于所有情况(零除外),并且很容易用其他语言编码:
$length = ceil(log10(abs($number) + 1)
回答by Miko?aj
In my opinion, the best way is:
在我看来,最好的方法是:
$length = ceil(log10($number))
A decimal logarithm rounded up is equal to length of a number.
四舍五入的十进制对数等于数字的长度。
回答by Matt Bell
If you are using a web form, make sure you limit the text input to only hold 10 characters as well to add some accessibility (users don't want to input it wrong, submit, get a dialog about their mistake, fix it, submit again, etc.)
如果您使用的是网络表单,请确保将文本输入限制为仅包含 10 个字符,以添加一些可访问性(用户不想输入错误,提交,获得有关他们错误的对话框,修复它,提交再等等)
回答by Ibrahim Dal
Use intval function in loop, See this example
在循环中使用 intval 函数,请参阅此示例
<?php
$value = 16432;
$length=0;
while($value!=0) {
$value = intval($value/10);
$length++
}
echo "Length of Integer:- ".$length;
?>
回答by user3738926
If you are evaluating mobile numbers (phone numbers) then I would recommend not using an int as your chosen data type. Use a string instead because I cannot forsee how or why you would want to do math with these numbers. As a best practice, use int, floats, etc, when you want/need to do math. Use strings when you don't.
如果您正在评估手机号码(电话号码),那么我建议不要使用 int 作为您选择的数据类型。改用字符串,因为我无法预见您将如何或为什么要使用这些数字进行数学运算。作为最佳实践,当您想要/需要进行数学运算时,请使用 int、float 等。不用时使用字符串。
回答by Emeka Augustine
From your question, "You want to get the lenght of an integer, the input will not accept alpha numeric data and the lenght of the integer cannot exceed 10. If this is what you mean; In my own opinion, this is the best way to achieve that:"
从你的问题,“你想得到一个整数的长度,输入不接受字母数字数据,整数的长度不能超过 10。如果这是你的意思;在我看来,这是最好的方法实现这一目标:"
<?php
$int = 1234567890; //The integer variable
//Check if the variable $int is an integer:
if (!filter_var($int, FILTER_VALIDATE_INT)) {
echo "Only integer values are required!";
exit();
} else {
// Convert the integer to array
$int_array = array_map('intval', str_split($int));
//get the lenght of the array
$int_lenght = count($int_array);
}
//Check to make sure the lenght of the int does not exceed or less than10
if ($int_lenght != 10) {
echo "Only 10 digit numbers are allow!";
exit();
} else {
echo $int. " is an integer and its lenght is exactly " . $int_lenght;
//Then proceed with your code
}
//This will result to: 1234556789 is an integer and its lenght is exactly 10
?>
回答by Matt Kirman
$input = "03432 123-456"; // A mobile number (this would fail)
$number = preg_replace("/^\d/", "", $number);
$length = strlen((string) $number);
if ($number == $input && $length == 10) {
// Pass
} else {
// Fail
}