PHP:将字符串转换为整数而不删除前导零
我有一个输入字段,其中包含用于 chmod 的文件模式。
如果我使用它,它会被用作字符串,所以它会失败。如果我将其转换为 int (intval()),它会删除前导零 (0777 => 777) 并再次失败。如果我像这样使用它:
$int = intval($input);
$finished = 0 . $int;
这也会失败,因为 is_int($finished)
为 false。
我该如何解决这个问题?
I've got an input field which contains the file mode used for chmod.
If I use this, it is used as string, so it fails. It I convert it to int (intval()), it deletes the leading zero (0777 => 777) and fails again. If I use it like this:
$int = intval($input);
$finished = 0 . $int;
This also fails because is_int($finished)
is false.
How can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于整数来说,前导零是一个不存在的概念,因为它们在数学上是微不足道的。在十进制表示法中,整数
0777
简单且完全等于777
。但是,如果您尝试将八进制表示法的
"0777"
转换为其对应的整数(511
作为十进制)以与chmod()
一起使用>,您可以使用octdec($input)
。该函数接受一个字符串并输出一个整数,完全按照它所说的那样进行。当然,请确保首先执行验证,例如使用正则表达式。您不希望将全局或无效标志交给
chmod()
并可能将您的敏感文件和文件夹暴露给公众。Leading zeroes are a nonexistent concept for integers, because they are mathematically insignificant. In decimal notation, the integer
0777
is simply and exactly equal to777
.But if you're trying to convert
"0777"
in octal notation to its integer counterpart (511
as a decimal) for use withchmod()
, you can useoctdec($input)
. The function takes a string and spits out an integer, doing exactly what it says on the tin.Of course, be sure you perform validation first, e.g. by using a regex. You don't want to hand a global or invalid flag to
chmod()
and potentially expose your sensitive files and folders to the public.您正在使用输入字段来获取
$input
。这意味着该值是一个字符串。您正在该值中查找八进制数。
intval
不会将字符串处理为八进制数字。chmod
期望$mode
参数为整数值。要转换包含八进制数字的字符串,可以使用
octdec
函数:可能想要验证输入并检查它是否也符合八进制数的条件,例如通过使用
再次转换它煎煮
并测试是否结果是一样的:You're using an input field to obtain
$input
. That means the value is a string then.You're looking for an octal number inside that value.
intval
is not handling strings as octal numbers.chmod
expects the$mode
parameter to be an integer value.To convert a string containing an octal number, you can use the
octdec
function:So you might want to validate the input and check if it does qualify as an octal number as well, for example by converting it again with
decoct
and testing if it results in the same:尝试使用 preg_match() 检查输入。参考下面的代码。
谢谢。
Try to check input with preg_match(). refer below code.
Thanks.
没什么好解决的,整数类型不能以0开头,除非等于0。0777按值等于777,通常将权限设置为777是没有问题的,系统会知道将其视为0777。
如果你必须传递一个前导零,它必须是一个字符串(例如
'0'.$finished
)Shai。
There is nothing to solve, an integer type can't start with 0 unless its equal to 0. 0777 is equal to 777 by value, Usually theres no problem setting a permission as 777, the system will know to treat it as 0777.
If you MUST pass a leading zero, it must be as a string (e.g.
'0'.$finished
)Shai.
当您使用函数 0 时。 $整数; php理解就是两个字符串相加的操作。所以结果
$finished 将是一个字符串。尝试使用函数number_format来解决。
When you use function 0 . $int; php understand that is a operation add two string. So result
$finished will be a string. try to use function number_format to solve.