<?

function text($id, $link) {

  $q = "SELECT texts.html FROM texts WHERE id=$id LIMIT 1";
  $result = mysql_query($q) or die(mysql_error());

  $row = mysql_fetch_row($result);
  return $row[0];

}

  $link = mysql_connect("localhost", "feeds", "feeds") or die(mysql_error());
  mysql_select_db("feeds") or die(mysql_error());

  $q = "SET NAMES 'utf8'";
#  $result = mysql_query($q) or die(mysql_error());

 $q = 'SELECT DISTINCT(blogs.title),feeds.url,feeds.date,feeds.time,texts.title,blogs.url,feeds.id FROM blogs,feeds,texts WHERE feeds.bid = blogs.id AND blogs.lang = "swedish" AND feeds.id = texts.id AND feeds.date = CURDATE() AND blogs.blog="Y" AND feeds.time < CURTIME() ORDER BY feeds.time DESC LIMIT 40';
  $result = mysql_query($q) or die(mysql_error());

  header("Content-Type: application/xml");

  $buf="<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>
 <rss version=\"2.0\">

 <channel>
  <title>bloggnytt.se</title>
  <link>http://bloggnytt.se</link>
  <description>Nyligen uppdaterade bloggar</description>
  <language>sv</language>\n\n";
  print $buf;

  while($row = mysql_fetch_row($result)) {

        $blogtitle = strip_tags($row[0]);
        $feedurl = strip_tags($row[1]);
        $date = strip_tags($row[2]);
        $time = strip_tags($row[3]);
        $feedtitle = strip_tags($row[4]);
        $blogurl = strip_tags($row[5]);
        $fid = $row[6];

        $replace="";
	$pattern="/([[:alnum:][:space:][:punct:]åäöÅÄÖ]*)/";
        $feedtitle=str_replace(str_split(preg_replace($pattern,$replace,$feedtitle)),$replace,$feedtitle);
        $feedurl=str_replace(str_split(preg_replace($pattern,$replace,$feedurl)),$replace,$feedurl);
        $blogtitle=str_replace(str_split(preg_replace($pattern,$replace,$blogtitle)),$replace,$blogtitle);


        $feedtitle = htmlspecialchars($feedtitle, ENT_NOQUOTES);
        $blogtitle = htmlspecialchars($blogtitle, ENT_NOQUOTES);
        $feedurl = htmlspecialchars($feedurl, ENT_NOQUOTES);
	$feedurl = "http://bloggnytt.se/go.php?fid=$fid";

        $text = text($fid, $link);
        $text = strip_tags($text);
        $text = substr($text, 0, 300);

        if(!$text) {
                $text = "Kan ej hitta text";
        }

        $text = "$text...";

          print(" <item>\n");
          print("  <title>$blogtitle - $feedtitle</title>\n");
          print("  <description>$text</description>\n");
          print("  <link>$feedurl</link>\n");
          print("  <guid>$feedurl</guid>\n");
          print(" </item>\n");

  }
 $buf = "</channel>

 </rss>";
 print $buf;

?>

