我正在尝试通过 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/