You are here:  » write parsing result to file

Support Forum



write parsing result to file

Submitted by fraizor on Mon, 2016-08-08 12:26 in

Hi, im new here , and im new to php
im trying to parse a page and write the output to a txt file

<?php
  
require("MagicParser.php");
  
$txt "";
  function 
myRecordHandler($record)
  {
   if (isset(
$record["TITLE"])) {
       
$txt $txt $record["TITLE"]."|;|";
   } else {
       
$txt $txt "False"."|;|" ;
   }
    if (isset(
$record["DESCRIPTION"])) {
       
$txt $txt $record["DESCRIPTION"]."|;|";
   } else {
       
$txt $txt "False"."|;|" ;
   }
   if (isset(
$record["LINK"])) {
       
$txt $txt $record["LINK"]."|;|";
   } else {
       
$txt $txt "False"."|;|" ;
   }
   if (isset(
$record["MEDIA:THUMBNAIL-URL"])) {
       
$txt $txt $record["MEDIA:THUMBNAIL-URL"]."|;|";
   } else {
       
$txt $txt "False"."|;|" ;
   }
     if (isset(
$record["PUBDATE"])) {
       
$txt $txt $record["PUBDATE"]."|:|" "<br>";
   } else {
       
$txt $txt "False"."|:|""<br>" ;
   } 
    echo 
"hi";
  }
  
$url "http://feeds.bbci.co.uk/arabic/rss.xml";
  
MagicParser_parse($url,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
  
$myfile fopen("test.txt""w") or die("Unable to open file!");
  
fwrite($myfile$txt);
  
fclose($myfile);
  echo 
$txt;
?>

what im doing wrong ?

Submitted by support on Mon, 2016-08-08 12:30

Hello fraizor and welcome to the forum!

I think the only problem is that $txt is not declared as global at the top of the myRecordhandler() function, so the $txt that is being constructed is a new variable for every record and not visible outside the function. Have a go with:

<?php
  
require("MagicParser.php");
  
$txt "";
  function 
myRecordHandler($record)
  {
   global 
$txt;
   if (isset(
$record["TITLE"])) {
       
$txt $txt $record["TITLE"]."|;|";
   } else {
       
$txt $txt "False"."|;|" ;
   }
    if (isset(
$record["DESCRIPTION"])) {
       
$txt $txt $record["DESCRIPTION"]."|;|";
   } else {
       
$txt $txt "False"."|;|" ;
   }
   if (isset(
$record["LINK"])) {
       
$txt $txt $record["LINK"]."|;|";
   } else {
       
$txt $txt "False"."|;|" ;
   }
   if (isset(
$record["MEDIA:THUMBNAIL-URL"])) {
       
$txt $txt $record["MEDIA:THUMBNAIL-URL"]."|;|";
   } else {
       
$txt $txt "False"."|;|" ;
   }
     if (isset(
$record["PUBDATE"])) {
       
$txt $txt $record["PUBDATE"]."|:|" "<br>";
   } else {
       
$txt $txt "False"."|:|""<br>" ;
   }
    echo 
"hi";
  }
  
$url "http://feeds.bbci.co.uk/arabic/rss.xml";
  
MagicParser_parse($url,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
  
$myfile fopen("test.txt""w") or die("Unable to open file!");
  
fwrite($myfile$txt);
  
fclose($myfile);
  echo 
$txt;
?>

Hope this helps!
Cheers,
David
--
MagicParser.com

Submitted by fraizor on Mon, 2016-08-08 22:20

Perfect thanks a lot