Basic File Analysis
This example shows a very simple file analysis script that can be used to determine what format string to use for a given source file and what field names are contained within the associative array passed to your record handler function for each record. Just like the demo script on this website it uses PHP's foreach construct to loop through each field in $record.
Assuming that autodetection is successful this demo just displays the contents of the first record in the source file. Click here to view the live output from this script. Remember that you could change $filename to be anything you like, such as the path to a local file rather than an Internet URL (an RSS feed in this example).
<?php
require("MagicParser.php");
$filename = "http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml";
$format_string = MagicParser_getFormat($filename);
if (!$format_string)
{
print "<p>".MagicParser_getErrorMessage()."</p>";
exit;
}
else
{
print "<p><strong>Autodetected Format String:</strong> ".$format_string."</p>";
}
print "<p>Contents of first record:</p>";
function myRecordHandler($record)
{
print "<table border='1'>";
foreach($record as $key => $value)
{
print "<tr>";
print "<th>".$key."</th>";
print "<td>".htmlentities($value)." </td>";
print "</tr>";
}
print "</table>";
// return a non-zero value to stop reading any more records
return 1;
}
MagicParser_parse($filename,"myRecordHandler",$format_string);
?>