Hello-
I am attempting to use magic parser to parse data which is presented in the following format:
http://www.ndbc.noaa.gov/data/realtime2/46232.spec
I have attempted to parse with the following:
MagicParser_parse("http://www.ndbc.noaa.gov/data/realtime2/46232.spec","myRecordHandler","csv|32|0|0")
For some reason I am getting inconsistent results on the returned data lines. i.e Data will be in Field 14 and the next row of the recordset the data will be in Field 16. Any ideas why this is happening?
Secondly would it be possible to just grab the 3rd line of the data only?
Hi,
The issue here is that some of the fields are separated by more than one space; so this increments the field counter moving the position of the data along unpredictably.
There is a simple solution, which is to read the data into a string; remove excessive white space using preg_replace; and then handing the string to Magic Parser using the "string://" operator. In addition the first 2 lines can be skipped using the optional 4th parameter in the Format String (skip lines), and then if you only want the first actual record (3rd line) returned, you can simply return TRUE from your record handler function to stop the parse.
Here's a complete example:
<?php
require("MagicParser.php");
function myRecordHandler($record)
{
print_r($record);
return TRUE;
}
$data = file_get_contents("http://www.ndbc.noaa.gov/data/realtime2/46232.spec");
$data = preg_replace( "/\s\s+/",' ',$data);
MagicParser_parse("string://".$data,"myRecordHandler","csv|32|0|0|2");
?>
Hope this helps!
Cheers,
David.