gpt4 book ai didi

php - 如何从文本文档中获取 2 个数据点?

转载 作者:行者123 更新时间:2023-11-28 03:41:19 25 4
gpt4 key购买 nike

我将网页的源代码复制到文本文档中,但无法从文件中获取两个数据点;纬度和经度。

我必须制作并扫描文档的 php 文件是这样的:

<?php

$ch = curl_init("http://www.marinetraffic.com/ais/shipdetails.aspx?MMSI=258245000");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);

header('Content-Type: text/plain');

$myFile = "example_homepage.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 9251);
fclose($fh);
echo $theData;

?>

gps 隐藏在如下所示的文本中(来自文件 example_homepage.txt):

<img style="border: 1px solid #aaa" src="flags/NO.gif" />
<br/>
<b>Call Sign:</b>LAJW
<br/>
<b>IMO:</b>9386380,
<b>MMSI:</b>258245000
<br/>
<hr/>
<h2>Last Position Received</h2>
<b>Area:</b>North Sea
<br/>
<b>Latitude / Longitude:</b>
<a href='default.aspx?mmsi=258245000&centerx=5.311533&centery=60.39997&zoom=10&type_color=9'>60.39997˚ / 5.311533˚ (Map)</a>
<br/>
<b>Currently in Port:</b>
<a href='default.aspx?centerx=5.32245&centery=60.39085&zoom=14'>BERGEN</a>
<br/>
<b>Last Known Port:</b>
</b>
<a href='default.aspx?centerx=5.32245&centery=60.39085&zoom=14'>BERGEN</a>
<br/>
<b>Info Received:</b>0d 0h 20min ago
<br/>
<table>
<tr>
<td>&nbsp;
<img src="shipicons/magenta0.png" />
</td>
<td>
<a href='default.aspx?mmsi=258245000&centerx=5.311533&centery=60.39997&zoom=10&type_color=9'><b>Current Vessel's Track</b></a>
</td>
</tr>
<tr>
<td>
<img src="windicons/w05_330.png" />
</td>
<td>
<b>Wind:</b>5 knots, 327&deg;, 13&deg;C</td>
</tr>
</table>
<a href='datasheet.aspx?datasource=ITINERARIES&MMSI=258245000'><b>Itineraries History</b></a>
<br/>
<hr/>
<h2>Voyage Related Info (Last Received)</h2>
<b>Draught:</b>6.8 m
<br/>
<b>Destination:</b>BERGEN HAVN
<br/>
<b>ETA:</b>2012-05-22 18:00
<br/>
<b>Info Received:</b>2012-05-23 18:43 (

我想要的两个数字是:

纬度:60.39085经度:5.32245

我对这种事情不是很有经验。也许有更好的方法。请告诉我。

编辑:仅供引用最后三行代码,我能够获得文本文件中的前 9251 个字符。

最佳答案

这可能有点矫枉过正,但你可以试试 PHP DOM + parse_url + parse_str :

$text = file_get_contents('http://example.com/path/to/file.html');
$doc = new DOMDocument('1.0');
$doc->loadHTML($text);
foreach($doc->getElementsByTagName('div') AS $div) {
$class = $div->getAttribute('class');
if(strpos($class, 'news') !== FALSE) {
if($div->hasAttribute('src') OR $div->hasAttribute('href')) {
$parsed_url = parse_url($div->getAttribute('src')));
$query_values = parse_str($parsed_url);
$desired_values = array(
$query_values['centerx'],
$query__values['centery']
);
}
}
}

关于php - 如何从文本文档中获取 2 个数据点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10759831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com