gpt4 book ai didi

php - 评估()不工作?

转载 作者:行者123 更新时间:2023-12-03 23:08:52 29 4
gpt4 key购买 nike

我正在尝试通过 PHP 从图像的 EXIF 数据中获取焦距。

这是我目前得到的代码:

$exif = exif_read_data("$photo");
$length10 = $exif['FocalLength'];
$length = eval($length10);

在这种情况下,$length10 返回类似于“1050/10”的 105mm。我不知道为什么。我只想让 PHP 计算返回 105。但是,当我运行它时,我收到以下错误消息:

[04-Nov-2012 20:06:39] PHP Parse error:  syntax error, unexpected $end in index.php(52) : eval()'d code on line 1

为什么?

最佳答案

因为 1050/10 不是有效的 PHP。它没有终止 ; 来结束语句,并导致语法错误。

php > eval("1050/10");
PHP Parse error: syntax error, unexpected end of file in php shell code(1) : eval()'d code on line 1

而不是 eval() 它(这在技术上是危险的,因为您正在有效地处理 用户输入 即使它来自 EXIF),建议拆分/ 或用正则表达式捕获操作数,然后自己执行操作。

// Test if the value matches the division pattern
if (preg_match('~^(\d+)/(\d+)$~', $length10, $operands)) {
// Following a successful match, $operands is an array
// containing the full matched string and the two numbers captured
// in indices [1],[2]

// Watch for div by zero!
if ($matches[2] !== 0) {
echo $operands[1] / $operands[2];
}
}
else {
echo $length10;
}

关于php - 评估()不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13225180/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com