Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: RSS and PHP

  1. #1
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    RSS and PHP

    I have this script that a friend wrote for me but I have since lost contact with him and need some help.

    The script is designed to take an RSS feed and generate a html file based on the content of that feed.

    for example if there is a feed with a title "rss feed today" then it should create a file name rss-feed-today.php.

    It seems to work but the only thing it does wrong is that it adds several tabs to the end of the file name e.g "rss-feed-today .php" and then the index page can reference it.

    Please can some help with the removal of the spaces.

    Regards

    Mike

    The code is:

    <?
    error_reporting(0);
    $metaTagsHTML ="";
    function startElement($parser, $tagName, $attrs) {
    global $insideitem, $tag;
    if ($insideitem) {
    $tag = $tagName;
    } elseif (($tagName == "ITEM") || ($tagName == "IMAGE")) {
    $insideitem = true;
    }
    }
    function endElement($parser, $tagName) {
    global $insideitem, $tag, $title, $description, $link, $iurl,$pubDate, $i, $n, $metaTagsHTML;
    if ($i >= $n) {
    return;
    }
    // Display Image elements [Edit markup below to customise output]
    if ($tagName == "IMAGE") {
    // printf("<div class=\"rss_img\"><a href='%s'>", trim($link));
    // printf("<img src='%s' border='0' />", $iurl);
    // printf("</a></div>");
    $title = "";
    $description = "";
    $link = "";
    $iurl = "";
    $pubDate = "";
    $insideitem = false;
    }
    // Display text CDATA [Edit markup below to customise output]
    if ($tagName == "ITEM") {


    $tempTitle = htmlspecialchars(trim($title));
    $newsHTML = "<?php include('../header.php') ?>
    <div align='center'>
    <table width='934' border='1'>
    <tr>
    <td><span class='style1'>$title</span></td>
    </tr>
    <tr>
    <td height='104'>
    <p>$description</p>
    <p class='style2'><a href='$link' class='style2'>Read</a></p></td>
    </tr>
    </table>
    </div>
    <?php include('../footer.php') ?>
    ";
    //print $newsHTML;
    $metaTagsHTML = $metaTagsHTML . "<meta name=\"keywords\" content=\"".$title ."\">
    \r\n";
    saveFile($newsHTML,$title,getDir($pubDate),$description);
    saveNewsListFile($title,$pubDate);

    $title = "";
    $description = "";
    $link = "";
    $iurl = "";
    $pubDate = "";
    $insideitem = false;
    $i += 1;
    }

    }
    function characterData($parser, $data) {
    global $insideitem, $tag, $title, $description, $link, $iurl, $pubDate;
    if ($insideitem) {

    switch ($tag) {
    case "TITLE":
    $title .= $data;
    break;
    case "DESCRIPTION":
    $description .= $data;
    break;
    case "LINK":
    $link .= $data;
    break;
    case "URL":
    $iurl .= $data;
    case "PUBDATE":
    $pubDate .= $data;
    break;
    }
    }
    }
    function parse_feed ($url, $num) {
    global $insideitem, $tag, $title, $description, $link, $iurl,$pubDate, $i, $n;

    $insideitem = false;
    $tag = "";
    $title = "";
    $description = "";
    $link = "";
    $iurl = "";
    $pubDate = "";
    $i = 0;
    $n = $num;

    // Prepare and create the expat parser.
    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    $fp = fopen($url,"r")
    or die("Error reading RSS data.");
    while ($data = fread($fp, 4096)) {
    xml_parse($xml_parser, $data, feof($fp))
    or die(sprintf("XML error: %s at line %d",
    xml_error_string(xml_get_error_code($xml_parser)),
    xml_get_current_line_number($xml_parser)));
    }
    fclose($fp);
    xml_parser_free($xml_parser);
    $i = 0;
    }

    function saveFile($str,$fileName,$dir,$dis){
    saveIndexFile($str,$fileName,$dir,$dis);
    $fp=fopen($dir."/".getValidFileName($fileName).".php","w");
    fwrite($fp,$str);
    fclose($fp);

    }
    function saveIndexFile($str,$fileName,$dir,$dis){

    $file = fopen("index.txt",'r+');
    $arr = array();
    $lCount = 0;
    $tempStr = "";

    while($line = fgets($file)){

    if($lCount==0){
    $lCount++;
    continue;
    }

    $tempStr= $tempStr . $line;

    }

    $title = str_replace("\n", "", $fileName);
    $disTemp = str_replace("\n", "", $dis);

    $tempStr = $tempStr ."\r\n".$dir."/".getValidFileName($fileName).".php:#:" . $title . ":#:" .substr($disTemp,0,70);
    fclose($file);
    $file2 = fopen("index.txt",'w');

    fwrite($file2,$tempStr);
    fclose($file2);

    }

    function saveNewsListFile($title,$date){
    $fileName = getValidFileName($title).".php";

    $file = fopen(getDir($date)."/newsList.txt",'r+');
    $arr = array();
    $tempStr = "";
    $title = str_replace("\n", "", $title);
    $date = str_replace("\n", "", $date);
    while($line = fgets($file)){

    $teampArray = split(":#:",$line);

    if($teampArray[0]==$title)
    continue;

    $tempStr= $tempStr . $line;

    }

    $tempStr = $tempStr ."\r\n".$title.":#:" . $fileName . ":#:" . $date;

    fclose($file);
    $file2 = fopen(getDir($date)."/newsList.txt",'w');

    fwrite($file2,$tempStr);
    fclose($file2);

    }

    function getValidFileName($fileName){
    $fileName = str_replace(" ", "-", $fileName);
    $fileName = str_replace(".", "", $fileName);
    $fileName = str_replace(":", "", $fileName);
    $fileName = str_replace(";", "", $fileName);
    $fileName = str_replace("/", "", $fileName);
    $fileName = str_replace("\\", "", $fileName);
    $fileName = str_replace("*", "", $fileName);
    $fileName = str_replace("<", "", $fileName);
    $fileName = str_replace(">", "", $fileName);
    $fileName = str_replace(",", "", $fileName);
    $fileName = str_replace("\n", "", $fileName);
    $fileName = str_replace("'", "", $fileName);
    $fileName = str_replace("\"", "", $fileName);
    $fileName = str_replace("?", "", $fileName);

    return $fileName;

    }
    function getDir($dateStr){
    $date = strtotime($dateStr,8);
    return date("F",$date);
    }
    function saveSchedul( $url ){
    $date = date("d/m/Y");

    $new = true;


    if($url!=""){

    $file = fopen("schedul.txt",'r+');
    while($line = fgets($file)){

    $teampArray = split(":#:",$line);
    $teampArray[1] = str_replace("\r\n", "", $teampArray[1]);

    if($teampArray[1]==$url){
    $new = false;
    }
    }

    fclose($file);


    if($new){
    $fname = "schedul.txt";
    $fhandle = fopen($fname,"r");
    $content = fread($fhandle,filesize($fname));
    $content = $content . $date.":#:".$url ."\r\n";
    $fhandle = fopen($fname,"w");
    fwrite($fhandle,$content);
    fclose($fhandle);
    }


    }


    }
    //print $today = date("d/m/y"); ;
    function checkSchedul(){
    global $metaTagsHTML;
    $file = fopen("schedul.txt",'r+');
    $arr = array();
    $tempStr = "";
    $currDate = date("d/m/Y");

    while($line = fgets($file)){

    $teampArray = split(":#:",$line);

    if($teampArray[0]!="\r\n" && compareDate ($currDate,$teampArray[0])>0){
    $tempStr = $tempStr . $currDate . ":#:" . $teampArray[1];
    $teampArray[1] = str_replace("\r\n", "", $teampArray[1]);
    parse_feed ($teampArray[1],25);

    continue;
    }



    $tempStr= $tempStr . $line;

    }

    $file2 = fopen("schedul.txt",'w');

    fwrite($file2,$tempStr);
    fclose($file2);
    fclose($file);
    if($metaTagsHTML!="" && $metaTagsHTML!=null){
    saveMetaTags();
    }


    }
    function compareDate ($i_sFirstDate, $i_sSecondDate)
    {
    //Break the Date strings into seperate components
    $arrFirstDate = explode ("/", $i_sFirstDate);
    $arrSecondDate = explode ("/", $i_sSecondDate);

    $intFirstDay = $arrFirstDate[0];
    $intFirstMonth = $arrFirstDate[1];
    $intFirstYear = $arrFirstDate[2];

    $intSecondDay = $arrSecondDate[0];
    $intSecondMonth = $arrSecondDate[1];
    $intSecondYear = $arrSecondDate[2];


    // Calculate the diference of the two dates and return the number of days.


    $intDate1Jul = gregoriantojd($intFirstMonth, $intFirstDay, $intFirstYear);
    $intDate2Jul = gregoriantojd($intSecondMonth, $intSecondDay, $intSecondYear);

    return $intDate1Jul - $intDate2Jul;

    }//end Compare Date

    function getValidDisName($fileName){

    $fileName = str_replace("/", "", $fileName);
    $fileName = str_replace("\\", "", $fileName);

    $fileName = str_replace("<", "", $fileName);
    $fileName = str_replace(">", "", $fileName);

    $fileName = str_replace("\n", "", $fileName);

    $fileName = str_replace("\"", "", $fileName);

    return $fileName;

    }
    function getIndexNews(){
    $file = fopen("index.txt",'r');
    $arr = array();
    while($line = fgets($file)){
    $teampArray = split(":#:",$line);
    echo " <p align='left'><a href='".$teampArray[0]."'>".$teampArray[1]."</a>
    <br />
    ".getValidDisName($teampArray[2])."...
    </p>";

    }
    fclose($file);
    }
    function saveMetaTags(){
    global $metaTagsHTML;


    $file = fopen("header.php",'r+');
    $arr = array();
    $tempStr = "";
    $flag = 0;

    while($line = fgets($file)){

    if($line=="<!--SEOStart-->\r\n"){
    $flag = 1;

    }

    if($line=="<!--SEOEnd-->\r\n")
    $flag = 2;

    if($flag==0){
    $tempStr= $tempStr . $line;

    }
    if($flag==2){
    $tempStr= $tempStr . "<!--SEOStart-->\r\n" . $metaTagsHTML . "<!--SEOEnd-->\r\n";
    $flag=0;
    }

    }
    $file2 = fopen("header.php",'w');

    fwrite($file2,$tempStr);
    fclose($file2);
    fclose($file);

    }
    ?>

  • #2
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is the link in action

    http://singles247.co.uk/dating_news/

    The two links on this page should link to the file it created however the files the script actually created are named the same but with the extra tabs so cant be accessed.

    I can rename the file manually and then they display correctly

  • #3
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    please read the Rules and Posting Guidelines
    and wrap the code in the php tags
    it makes it much easier to read
    like so
    PHP Code:
    echo = "$somevar"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •