FAQ |
Kalender |
![]() |
#1 | |||
|
||||
Medlem
|
Hej jag har ett mycket bizarrt problem,
Första rubriken blir bra men sen dyker den upp igen ![]() exempel: http://rsseater.ppelgren .se/rsseater.php som ni ser så ser det ut såhär: CARS volvo bmw ford bently FOODCARS pizza burgers sushi volvo bmw ford bently Men det skall vara såhär: CARS volvo bmw ford bently FOOD pizza burgers sushi KOD: rss_reader.php Kod:
<?php /* set_time_limit(0); $rss_channel = array(); $currently_writing = ""; $main = ""; $item_counter = 0; function startElement($parser, $name, $attrs) { global $rss_channel, $currently_writing, $main; switch($name) { case "RSS": case "RDF:RDF": case "ITEMS": $currently_writing = ""; break; case "CHANNEL": $main = "CHANNEL"; break; case "IMAGE": $main = "IMAGE"; $rss_channel["IMAGE"] = array(); break; case "ITEM": $main = "ITEMS"; break; default: $currently_writing = $name; break; } } function endElement($parser, $name) { global $rss_channel, $currently_writing, $item_counter; $currently_writing = ""; if ($name == "ITEM") { $item_counter++; } } function characterData($parser, $data) { global $rss_channel, $currently_writing, $main, $item_counter; if ($currently_writing != "") { switch($main) { case "CHANNEL": if (isset($rss_channel[$currently_writing])) { $rss_channel[$currently_writing] .= $data; } else { $rss_channel[$currently_writing] = $data; } break; case "IMAGE": if (isset($rss_channel[$main][$currently_writing])) { $rss_channel[$main][$currently_writing] .= $data; } else { $rss_channel[$main][$currently_writing] = $data; } break; case "ITEMS": if (isset($rss_channel[$main][$item_counter][$currently_writing])) { $rss_channel[$main][$item_counter][$currently_writing] .= $data; } else { //print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>"); $rss_channel[$main][$item_counter][$currently_writing] = $data; } break; } } } function doRss($file) { global $rss_channel; $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); if (!($fp = fopen($file, "r"))) { die("could not open XML input"); } while ($data = fread($fp, 4096)) { if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } } xml_parser_free($xml_parser); // output as HTML /* print ("<html> <head> <title>RSS mataren</title> </head> <body> "); */ print ("<div class=\"ruta\"> <h3>" . $rss_channel["TITLE"] . "</h3> "); print ("<h4>" . $rss_channel["DESCRIPTION"] . "</h4> <ul> "); if (isset($rss_channel["ITEMS"])) { if (count($rss_channel["ITEMS"]) > 0) { for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) { if (strlen($rss_channel["ITEMS"][$i]["TITLE"]) > 50) $rss_channel["ITEMS"][$i]["TITLE"] = substr($rss_channel["ITEMS"][$i]["TITLE"], 0, 47) . '...'; print ("<li><a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\" target=\"_blank\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a>"); print ("</li> "); } } else { print ("<b>There are no articles in this feed.</b>"); } } print (" </ul> </div>"); /* print (" </body> </html>"); */ } ?> Kod:
<?php require('rss_reader.php'); doRss('http://feeds.reuters.com/reuters/topNews/'); doRss('http://rss.cnn.com/rss/cnn_topstories.rss'); doRss('http://www.nytimes.com/services/xml/rss/nyt/Automobiles.xml'); ?> |
|||
![]() |
![]() |
![]() |
#2 | ||
|
|||
Banned
|
Jag skulle nog se dig lösa detta själv...
Lek runt själv... Risken finns att du lär dig något faktiskt ![]() |
||
![]() |
![]() |
![]() |
#3 | |||
|
||||
Medlem
|
ush vad hemskt, lära sig något? det värsta jag hört på länge
![]() nu har jag löst det... |
|||
![]() |
![]() |
![]() |
#4 | ||
|
|||
Mycket flitig postare
|
Citat:
|
||
![]() |
![]() |
![]() |
#5 | ||
|
|||
Medlem
|
Citat:
Kod:
// ============= creates a tree (array) from the given xml data function xml2array($text) { $reg_exp = '/<(\w+)[^>]*>(.*?)<\/>/s'; preg_match_all($reg_exp, $text, $match); foreach ($match[1] as $key=>$val) { if ( preg_match($reg_exp, $match[2][$key]) ) { $array[$val][] = xml2array($match[2][$key]); } else { $array[$val] = $match[2][$key]; } } return $array; } |
||
![]() |
![]() |
Svara |
|
|