分析文件中的数据后使用舍入函数
Use rounding function after parsing data from a file
我有很多html文件,其中有几个链接。例如
<div><a href="/location/?latitude=41.7948205&longitude=-72.12729890000003" >location 1</a></div>
<div><a href="/location/?latitude=41.9020418&longitude=-72.07979440000002" >location 2</a></div>
我想把经度值四舍五入到小数点后6位。例如
<div><a href="/location/?latitude=41.7948205&longitude=-72.127299" >location 1</a></div>
<div><a href="/location/?latitude=41.9020418&longitude=-72.079794" >location 2</a></div>
当涉及到文件解析时,我是一个傻瓜,不知道这是否可能。如果有任何建议和指导,我将不胜感激。
下面是一些可以做到这一点的PHP代码:
$str = '<div><a href="/location/?latitude=41.7948205&longitude=-72.12729890000003" >location 1</a></div>
<div><a href="/location/?latitude=41.9020418&longitude=-72.07979440000002" >location 2</a></div>
<div><a href="/location/?latitude=41.9020418&longitude=-72.07979440000002" >location 3</a></div>';
$arr = explode("n", $str);
$decimalPlaces = 5;
foreach ($arr as &$line) {
if (preg_match("/latitude=(.*?)&longitude=(.*?)"/", $line, $matches)) {
$latitudeApprox = round($matches[1], $decimalPlaces);
$longitudeApprox = round($matches[2], $decimalPlaces);
$line = preg_replace("/latitude=.*?&longitude=.*?"/", "latitude=$latitudeApprox&longitude=$longitudeApprox"", $line);
}
}
$str = implode("n", $arr);
echo $str;
请注意,用于提取经度的正则表达式不是很健壮。它假设你从未遇到过这样的情况,例如:
<a href="/location/?latitude=41.7948205&longitude=-72.12729890000003&SOMETHING+ELSE+HERE" >...</a>
相关文章:
- C++将浮点指针值舍入为小数位数
- Boost::posix_time::ptime舍入到给定的分钟数
- 浮点定向舍入和优化
- 为什么输出精度没有正确舍入?
- 使用设置精度时如何阻止数字向上舍入?
- 如何在使用 MPFR 时在提升多精度中设置舍入模式
- OpenCV 细分一致的 Rect2f 到 Rect2i 舍入
- 每个线程 C++ 保护以防止重入函数调用
- C++ 中的舍入函数
- 舍入 QDecDouble 值,精度最多为两个字符
- 如何将最小值和最大值放入函数中
- 将大 int 转换为浮点数,而不舍入 c++
- 舍入错误检测
- 是否可以为 AVR 编写一个 constexpr 舍入函数?
- 在C++函数中发生奇怪的舍入
- 我可以用标准库组件构建这个特殊的舍入函数吗
- 在C++中使用地板函数的舍入误差
- 分析文件中的数据后使用舍入函数
- 从而创建舍入函数
- 是否有标准的格式化函数或运算符进行舍入?(?)