Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 解析文件中的数据后使用舍入函数_Php_C++_C_Parsing_Rounding - Fatal编程技术网

Php 解析文件中的数据后使用舍入函数

Php 解析文件中的数据后使用舍入函数,php,c++,c,parsing,rounding,Php,C++,C,Parsing,Rounding,我有很多html文件,其中有几个链接。例如 <div><a href="/location/?latitude=41.7948205&amp;longitude=-72.12729890000003" >location 1</a></div> <div><a href="/location/?latitude=41.9020418&amp;longitude=-72.07979440000002" >loc

我有很多html文件,其中有几个链接。例如

<div><a href="/location/?latitude=41.7948205&amp;longitude=-72.12729890000003" >location 1</a></div>
<div><a href="/location/?latitude=41.9020418&amp;longitude=-72.07979440000002" >location 2</a></div>

我想把经度值四舍五入到小数点后6位。例如

<div><a href="/location/?latitude=41.7948205&amp;longitude=-72.127299" >location 1</a></div>
<div><a href="/location/?latitude=41.9020418&amp;longitude=-72.079794" >location 2</a></div>


说到文件解析,我是个笨蛋,不知道这是否可行。我将非常感谢您的指点和指导。

以下是一些PHP代码:

$str = '<div><a href="/location/?latitude=41.7948205&amp;longitude=-72.12729890000003" >location 1</a></div>
<div><a href="/location/?latitude=41.9020418&amp;longitude=-72.07979440000002" >location 2</a></div>
<div><a href="/location/?latitude=41.9020418&amp;longitude=-72.07979440000002" >location 3</a></div>';

$arr = explode("\n", $str);

$decimalPlaces = 5;

foreach ($arr as &$line) {
     if (preg_match("/latitude=(.*?)&amp;longitude=(.*?)\"/", $line, $matches)) {
         $latitudeApprox = round($matches[1], $decimalPlaces);
         $longitudeApprox = round($matches[2], $decimalPlaces);
         $line = preg_replace("/latitude=.*?&amp;longitude=.*?\"/", "latitude=$latitudeApprox&amp;longitude=$longitudeApprox\"", $line);
    }
}

$str = implode("\n", $arr);
echo $str;
$str='1〕
';
$arr=爆炸(“\n”,$str);
$decimalPlaces=5;
foreach($arr as和$line){
if(preg_match(“/纬度=(.*?)&经度=(.*?\”/”,$line,$matches)){
$latitudeApprox=四舍五入($matches[1],$decimalPlaces);
$longitudeApprox=四舍五入($matches[2],$decimalPlaces);
$line=preg\u replace(“/latitude=.*?&;longitude=.*?\”/“,“latitude=$latitudeApprox&;longitudeaprox=$longitudeaprox\”,$line);
}
}
$str=内爆(“\n”,$arr);
echo$str;
请注意,用于提取经度的正则表达式不是非常健壮。它假设您从未遇到过这样的情况,例如:

<a href="/location/?latitude=41.7948205&amp;longitude=-72.12729890000003&amp;SOMETHING+ELSE+HERE" >...</a>


“太好了!”你喜欢快速的回答吗?谢谢你的快速反应。我对PHP、C和C++感到很满意。你是一个救生员。这很好!不,我没有任何情况,在经度之后,还有其他查询参数。如果我想同时在纬度和经度上来回旋转,那么这很容易修改吗?5小数点。当然,我更新了代码以同时提取经度和纬度。您可以将$decimalPlaces的值更改为您需要的任何精度。太好了!非常感谢!