PHP 解码数组


PHP decoding array

我一直在尝试从国家气象局获取潮汐信息。 这是我一直在玩的代码:

<?php
$homepage = file('http://opendap.co-ops.nos.noaa.gov/axis/webservices/highlowtidepred/response.jsp?stationId=8454000&beginDate=20150101&endDate=20151231&datum=MLLW&unit=0&timeZone=0&format=html&Submit=Submit');
$t2=explode("LST",$homepage[0]);
print_r($t2[1]);
echo "<br><br>";
$s =explode('item ',$t2[1]);
print_r($s[1]);
echo "<br><br>";
$u =explode("/time",$s[1]);
print_r($u);
echo "<br><br>";
$n=explode("<data>",$u[1]);
print_r($n);
echo "<br><br>";
$m=explode("<pred>",$n[0]);
print_r($m);
echo "<br><br>";
for($i = 0;$i<90;$i++)
{
   print_r ( $m[1][$i]);
   echo "<br>";
}

?>

这是输出:

04:414.848H12:010.395L17:094.352H22:08-0.105L05:404.96H11:010.257L18:044.48H22:.......
date="01/01/2015">04:414.848H12:010.395L17:094.352H22:08-0.105L<
Array ( [0] => date="01/01/2015">04:41< [1] => >4.848H12:01< [2] => >0.395L17:09< [3] =>   >4.352H22:08< [4] => >-0.105L< )
Array ( [0] => >4.848H [1] => 12:01< )
Array ( [0] => > [1] => 4.848H )
4
.
8
4
8
<
/
p
r
e
d

>
<
 t
 y
 p
e
>
H
<
/
t
y
p
e
>
<
/
d
a
t
a
>

当我print_r($m)时,我得到了我所期望的,但是当我打印出$m[1][$i]时,我在描述数据元素的<>之间嵌入了文本。 有没有一种简单的方法可以去除这些? 我以前从未见过这个,想知道在下面看什么来阅读这个。

我认为使用他们的 SOAP 服务来获取您正在寻找的数据会更容易。 至少,以XML格式而不是从HTML页面获取数据会容易得多。 下面是一个 SOAP 调用示例,它将为你获取与问题中的 HTML URL 相同的数据:

$wsdl = "http://opendap.co-ops.nos.noaa.gov/axis/webservices/highlowtidepred/wsdl/HighLowTidePred.wsdl";
$params = array("stationId" => "8454000", "beginDate" => "20150101", "endDate" => "20151231", "datum" => "MLLW", "unit" => 0, "timeZone" => 0);
$client = new SoapClient($wsdl);
$result = $client->__call("getHighLowTidePredictions", array("parameters" => $params));

$result现在应该是从 Web 服务返回的对象类型,并且比您当前正在使用的 HTML 更容易迭代。

最好使用适当的解析方法,如正则表达式、dom 甚至更好的 Web 服务,如 Mooseknuckles 建议的。

$wsdl = "http://opendap.co-ops.nos.noaa.gov/axis/webservices/highlowtidepred/wsdl/HighLowTidePred.wsdl";
$params = array(
        "stationId" => "8454000",
        "beginDate" => "20150101",
        "endDate" => "20151231",
        "datum" => "MLLW",
        "unit" => 0,
        "timeZone" => 0
);
$client = new SoapClient($wsdl);
$result = $client->__call("getHighLowTidePredictions", array("parameters" => $params));
$records = $result->HighLowValues->item;
foreach($records as $record){
    $date = $record->date;
    foreach($record->data as $data){
        $time = $data->time;
        $pred = $data->pred;
        $type = $data->type;
        // do something with this data here
        // ...
    }
}