我试图通过PHP从图像的EXIF数据中获取焦距.
这是我到目前为止的代码:
$exif = exif_read_data("$photo");
$length10 = $exif['FocalLength'];
$length = eval($length10);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,$ length10为105mm返回类似"1050/10"的东西.我不知道为什么.我想要做的就是让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
Run Code Online (Sandbox Code Playgroud)
为什么?
由于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
Run Code Online (Sandbox Code Playgroud)
而不是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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3642 次 |
| 最近记录: |