View Full Version : use php to turn html table data to php variables? RESOLVED

01-06-2012, 07:26 PM
hi, i am trying to extract a column of weather data and turn it into a line graph.

i have the graph part already sorted - it just needs raw data inputted as php variables.

the data comes from a html table at


in particular i am trying extract the data from the column called "valid time" and also the corresponding data from the "500-1000 THKNS" column.

the resultant variables would just be called $x and $y, for example the variables with data would end up looking like..

$x1 = Fri 01/06 15Z
$x2 = Fri 01/06 18Z
$x3 = Fri 01/06 21Z

etc etc

and also

$y1 = 544
$y2 = 544
$y3 = 545

how easy is this to do using php and could someone please help me?

01-06-2012, 08:36 PM
Before you go through all of this, did you ask them if they provide
the data in the form of a CSV (comma separated variable) or XML format?
If they did, that would be the best solution.

But if that HTML <table> is all they have for a data report, you're stuck
with parsing it all out. It can be done, but ask about the format first.
If they only have the HTML table, they should have someone program a
different format option ... which would be fairly easy on their side of it.

Let us know.

01-06-2012, 08:40 PM
thanks mate.

it is just a web site that I found and there is no evidence of csv else it would be linked.

how would I go about it with just the format above?

01-06-2012, 09:36 PM
There are two separate sections of 500-1000 THKNS ...
Do you need both sections?

And would it be OK to only pick the one's that are links (every other one?).
Picking the one with links is easy because those lines have a unique string in them.

01-06-2012, 09:48 PM
yes, unfortunately i do need both sections of THCKNS (as with the dates too)

ideally i'd like every value but if the linked ones are easier then so be it.


01-06-2012, 10:25 PM
So, below is my script.
It's not perfect. The only unique thing on the lines we want to parse are the "SLP_THK_RH" strings.
That means I have to grab the line before it too, just assuming it's a value without a link.
At the end, we don't know if the list ends with a "SLP_THK_RH" string or not, so the last value may be blank.

Anyhow, try this out.


// site url
$site_url = "http://wxweb.meteostar.com/sample/sample_C.shtml?text=EGRR/#";

// With my webhost, I am not allowed to use fopen() or get_contents.
// So, I use the preferred CURL method. This depends on your webhost and PHP revision.

// curl get contents
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $site_url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
$lines = array();
$lines = explode("\n", $file_contents);

// define a couple of arrays,
// one for the time, and the other for the actual value.

// loop thru and find the specific text within each line.
// the only thing to key from is "SLP_THK_RH", and they only
// appear on the lines with a link for the value.
foreach($lines as $line) {

// You'll miss the last value if it's not a link,
// because that line won't have "SLP_THK_RH" in the string.
// So, your data will be short by one value.
// We can't be sure if the last line will ever be a link or not.

// Display the actual arrays ...
echo "<br><br>";
echo "<br><br>";

// Display results ...
// Notice that time starts with one extra line because "Z<" appears on a table title line.
// So, the $time array is offset by 1.
for($x=0; $x<=count($value); $x++){
echo "TIME: {$time[$x+1]} ... VALUE: {$value[$x]} <br />";



You could also use Google Chart to create your line graph.
The PHP would be generating the javascripting "on-the-fly".
I do that same thing with Yahoo Weather. I process their XML
file and generate the javascripting for Google Chart:

You can view the HTML source to see how the values are put into the chart.


01-07-2012, 08:56 AM
thank you so much!

however, when i add

print $x1 $y1;

at the end of the code, it doesn't print them?

(my second code relies on the variables being created in this way)

eg if i call your code snow.php, my second code may be


include ("snow.php");

print $x1;
print $y1;


which expectedly returns your output again (as i didn't hash out the print bits of your code) but not the $x1 and $y1 as i added in?

can you please help?


01-07-2012, 09:23 AM

just realised they are stored in arrays, so i can call them with

echo $time[6];
echo $value[6];


01-07-2012, 10:24 AM
out of interest is there a way I can turn one of the array into a variable?

this seemed obvious but doesn't work.

$v1 == $value[4];

any ideas. I had a search but couldn't get a clear answer at my level.

01-07-2012, 01:23 PM
for referece for others i successfully used:

extract($time, EXTR_PREFIX_ALL, 'time');
extract($value, EXTR_PREFIX_ALL, 'value');

print " $value_0 $value_1 $value_2 $value_3 $value_4 ";
print " $time_0 $time_1 $time_2 $time_3 $time_4 ";

01-08-2012, 01:13 AM
You don't want to ... or shouldn't turn the array into variables ... why would you do that?

You can print the array values just like you do the "variables" you are trying to use.

I don't get it.

01-20-2012, 02:00 PM
Which temperature columns(s) are you trying to get?
Let me know what the column heading is on the ones you want.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum