/    Sign up×
Community /Pin to ProfileBookmark

parsing table data: columns and rows

Hi,
I have a mini webserver program that is an add-on to a program that I use. The server creates a table of the data it has in its database over the past 60 minutes. The table looks like this.

[code=html]<table align=”center” border=”2″ bgcolor=”white”>
<tr>
<td align=”left”><a href=”find.cgi?call=KC7VKP-9″>&nbsp;KC7VKP-9*</a></td><td align=”left”>Jeep</td><td align=”center”>42.13.13N 121.45.07W</td><td align=”right”>61.9</td><td align=”right”>116</td><td align=”left”>Feb 17 06:39</td>
</tr>
<tr>
<td align=”left”><a href=”find.cgi?call=KD7RY-7″>&nbsp;KD7RY-7*</a></td><td align=”left”>Truck 18wh</td><td align=”center”>42.36.35N 123.22.99W</td><td align=”right”>28.1</td><td align=”right”>268</td><td align=”left”>Feb 17 06:40</td>
</tr>
<tr>
<td align=”left”><a href=”find.cgi?call=KD7VUG-9″>&nbsp;KD7VUG-9*</a></td><td align=”left”>Car</td><td align=”center”>45.28.77N 123.50.68W</td><td align=”right”>203.7</td><td align=”right”>346</td><td align=”left”>Feb 17 06:24</td>
</tr>
<tr>
<td align=”left”><a href=”find.cgi?call=KE6WHN-3″>&nbsp;KE6WHN-3*</a></td><td align=”left”>Truck 18wh</td><td align=”center”>42.27.66N 123.19.14W</td><td align=”right”>27.2</td><td align=”right”>246</td><td align=”left”>Feb 17 06:39</td>
</tr>
<tr>
<td align=”left”><a href=”find.cgi?call=N2RSI-9″>&nbsp;N2RSI-9*</a></td><td align=”left”>Truck</td><td align=”center”>41.57.74N 121.55.27W</td><td align=”right”>65.2</td><td align=”right”>134</td><td align=”left”>Feb 17 06:33</td>
</tr>
<tr>
<td align=”left”><a href=”find.cgi?call=W7ZQD-7″>&nbsp;W7ZQD-7*</a></td><td align=”left”>Car</td><td align=”center”>44.05.07N 123.04.06W</td><td align=”right”>101.7</td><td align=”right”>353</td><td align=”left”>Feb 17 06:04</td>
</tr>
</table>[/code]

How can I use PHP to access this page via “http://10.9.8.5:81/page.html” and take the first column of data only but for all of the rows of data. The data that I want is the query in the link tag. I have already tried this but it only gets the first occerance of the find.

[code=php]$html = file_get_contents(‘http://10.9.8.5:81/mobilesrf.html’);
list($i,$ii) = split(‘call=’,$html);
list($i,$ii) = split(‘”>’,$ii);
echo $i;[/code]

Is there some other way that I can do this? I looked over the PHP manual last night and couldn’t see anything.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 17.2006 — [code=php]
$html = file_get_contents('http://10.9.8.5:81/mobilesrf.html');
preg_match_all('/?call=([^'"]+)['"]/iU', $html, $matches);
// values are now in array $matches[1]:
echo "<pre>";
print_r($matches[1]);
echo "</pre>n";
[/code]
Copy linkTweet thisAlerts:
@towerboyauthorFeb 17.2006 — Ok, that works but I am not sure how to format it in a table the way I want. I did figure out how to get it to work, and I know there is probably an easier way to do it, but it is a working starting point. This is what I have right now.
[code=php]$html = file_get_contents('http://10.9.8.5:81/mobilesrf.html');

$aprs = explode("<tr>", $html);

echo "<center>Here is a list of mobile APRS stations heard in southern Oregon.</center>";

echo "<table align='center' border='1'>";

foreach($aprs as $test){
list($i,$ii) = split('call=',$test);
list($i,$ii) = split('">',$ii);
if($i !== ""){
echo "<tr><td>";
echo $i;
echo "</td></tr>";
}
echo "</table>";[/code]

That seems to work fine. Next problem. Is there something that is stopping me from using this:
[code=php]$html = file_get_contents('http://10.9.8.5:81/mobilesrf.html');
$info = file_get_contents('http://10.9.8.5:81/wxstations.html');[/code]

I am always getting this error in the browser if I have two "file_get_contents" functions in the script and it doesn't matter where I place it or even if it is the same file.
[CODE]Fatal error: Maximum execution time of 30 seconds exceeded in C:Apache2htdocsaprsmobiles.php on line 3[/CODE]
Can I not use that command more than once in the same script?
Copy linkTweet thisAlerts:
@SpectreReturnsFeb 18.2006 — That just means it's taking too long to grab the content. Use [code=php]set_time_limit(0)[/code] to allow it to take as much time as it needs.
Copy linkTweet thisAlerts:
@towerboyauthorFeb 18.2006 — Hey that works great. thanks.

Is there any way to speed this up though? My pages used to be generated in 2.5 seconds. Now since I added this, it takes anywhere from 17-25 seconds per page load. The CPU on the server is only using 7%. Here is my code if anyone wants to take a look to see if they are able to figure it out.
[code=php]set_time_limit(0);
$html = file_get_contents("http://10.9.8.5:81/mobilesrf.html");
$aprs = explode("<tr>", $html);

list($cnt,$ii) = split('<stncnt>',$html);
list($cnt,$ii) = split('</stncnt>',$ii);

list($exp,$ii) = split('<expire>',$html);
list($exp,$ii) = split('</expire>',$ii);

echo "<center>There have been " .$cnt. " mobile stations heard on RF in the past " .$exp. " minutes.</center>rn";

echo "<table align='center' border='1'>rn";

foreach($aprs as $test){
list($i,$ii) = split('call=',$test);
list($i,$ii) = split('">',$ii);
if($i !== ""){
echo " <tr>rn <td>rn";
echo " ";
echo $i;
echo "rn </td>rn </tr>rn";
}
}
echo "</table>rn";[/code]

If you want to see what the output result is, click http://www.towerboy.com and look at the bottom left. I will only be able to leave this block up and running this slow for so long. Thanks.
Copy linkTweet thisAlerts:
@NogDogFeb 18.2006 — While it's probably not going to win any awards for code efficiency, it's still probably most likely that most of the delay is the file_get_contents. You might want to try something like the following just to ascertain this:
[code=php]
set_time_limit(0);
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$startTime1 = microtime_float();
$html = file_get_contents("http://10.9.8.5:81/mobilesrf.html");
$endTime1 = $startTime2 = microtime_float();
// rest of script here, then...
$endTime2 = microtime_float();
printf("<p>DEBUG: file_get_contents took approx. %.6f seconds<br>n",
$endTime1 - $startTime1);
printf("The rest of the script took %.6f seconds</p>n",
$endTime2 - $startTime2);
[/code]
Copy linkTweet thisAlerts:
@towerboyauthorFeb 18.2006 — Wow, thats cool. This is the result.
[CODE]DEBUG: file_get_contents took approx. 16.714214 seconds
The rest of the script took 0.000336 seconds[/CODE]

would fopen be better?
Copy linkTweet thisAlerts:
@NogDogFeb 18.2006 — I doubt it - the delay is probably a network/internet thing, nothing to do with your script.
Copy linkTweet thisAlerts:
@towerboyauthorFeb 18.2006 — When I open the file using fopen, it takes a half a second. when I echo the variable, this is what I get.
[CODE]Resource id #98[/CODE]
What does that mean? No, that's not the content I am after.
×

Success!

Help @towerboy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...