English 中文(简体)
如何从文本文档中获得两个数据点?
原标题:How to get 2 data points from a text document?
  • 时间:2012-05-25 18:45:43
  •  标签:
  • php
  • html

我将网页的源头复制为文本文件,我很难从文件中找到两个数据点:纬度和经度。

我要做的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 被埋在类似文字的文字中( 从文件示例_ 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 (

我要的两个数字是:

latitude: 60.39085 longitude: 5.32245

我对这种事情没有经验,也许有更好的办法,请告诉我。

使用最后三行代码后, 我就能在文本文件中找到第9251个字符。

最佳回答

这就是我为了得到我想要的结果而做的: (打印出 em>-7019347 42.02112 /em>)

<?php
//goes though and copies the web page to a text file
$ch = curl_init("http://photos.marinetraffic.com/ais/lightdetails.aspx?light_id=1000019773");
$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);

//prevents some parsing of the html document
header( Content-Type: text/plain );

//opens text file and reads contents to a string
$myFile = "example_homepage.txt";
$fh = fopen($myFile,  r );
$theData = fread($fh,12000);
fclose($fh);

//finds the location of the beginning of the GPS data
$pos = strrpos($theData, "&centerx=");
if ($pos === false) { 
    // note: three equal signs
    echo "not found";
}

//cuts out that string and finds position for x and y components
$subtract = 12000-$pos-36;
$rest = substr($theData, $pos, -$subtract);
$lat = substr($rest, 9, -17);
$lonpos = strrpos($rest, "&centery=")+9;
$lon = substr($rest, $lonpos);

//turns the values into floats
$lat = floatval($lat);
$lon = floatval($lon);

//echo $rest;
echo $lat;
echo " ";
echo $lon;

?> 

希望这能帮到某人

问题回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签