-1

RSS dúvida

criado por Cleiton Becker em 01/09/2009 4:33pm
Pessoal estou com um código que eu peguei aqui mesmo no portal.

Bom o problema é que eu estou precisando fazer com que pegue apenas 5 notícias e sejam exibidas pelo script, o que agora neste momento não está ocorrendo.

Já quebrei a cabeça e é a primeira vez que estou lidando com xml alguem pode medar uma ajuda de como deixar a opção ou onde colocar ou como fazer para limitas as linhas que eu pego?

Segue os código

<?php

$feed = 'http://www.tjrs.jus.br/site_php/noticias/news_rss.php&#039;;

ini_set('allow_url_fopen', true);

$fp = fopen($feed, 'r');

$xml = '';

while (!feof($fp)) {

$xml .= fread($fp, 128);
}

fclose($fp);

function untag($string, $tag){

$tmpval = array();
$preg = "|<$tag>(.*?)</$tag>|s";
preg_match_all($preg, $string, $tags);
foreach ($tags[1] as $tmpcont){
$tmpval[] = $tmpcont;
}

return $tmpval;
}

$items = untag($xml, 'item');

//echo count(items);

$html = '<p>';

foreach ($items as $item) {
//for($i=0;$i <= count(items);$i++){

$title = untag($item, 'title');
$descricao = untag($item, 'description');
$link = untag($item, 'link');

$html .= '<a href="' . $link[0] . '">' . $title[0] . "</a><br />\n";

}

$html .= '</p>';

echo $html;

?>

Lista de Respostas:

0
01/09/2009 4:48pm
(~14 anos atrás)
BOZO respondeu:
voce pode trocar isso
foreach ($items as $item) {

por isso
for($i=0;$i <= 5;$i++){



ps. acredito que esse tipo de duvida seria melhor voce postar no forum que em Faq...

0
01/09/2009 5:05pm
(~14 anos atrás)
Cleiton Becker respondeu:
sim isso eu tentei e não exibe nada.

0
01/09/2009 10:46pm
(~14 anos atrás)
BOZO respondeu:
da maneira que vc postou esta funcionando?

0
04/09/2009 2:10pm
(~14 anos atrás)
Cleiton Becker respondeu:
não ~simplesmente não aparece nada.

0
04/09/2009 3:12pm
(~14 anos atrás)
Cleiton Becker respondeu:
Buenas,

problema resolvido segue abaixo nada que um contator e um if() não resolva hehe

Obrigado pela ajuda.

<?php

function untag($string, $tag){

$tmpval = array();
$preg = "|<$tag>(.*?)</$tag>|s";
preg_match_all($preg, $string, $tags);
foreach ($tags[1] as $tmpcont){
$tmpval[] = $tmpcont;
}

return $tmpval;
}



//--------------------------------------------------------------------------------------------------------
// tribunal de justiça rio grande do sul
$feed = 'http://www.tjrs.jus.br/site_php/noticias/news_rss.php&#039;;

ini_set('allow_url_fopen', true);

$fp = fopen($feed, 'r');

$xml = '';

while (!feof($fp)) {

$xml .= fread($fp, 128);
}

fclose($fp);

$items = untag($xml, 'item');

$html = '<p>';

//foreach ($items as $item) {
//for($i=0;$i <= 5;$i++){
$count = 0;
foreach ($items as $item) {

if($count <= 4){

$title = untag($item, 'title');
$descricao = untag($item, 'description');
$link = untag($item, 'link');

$html .= '<a href="' . $link[0] . '">' . $title[0] . "</a><br />\n";


}
$count = $count + 1;
}

$html .= '</p>';

echo $html;



//---------------------------------------------------------------------------------------------------
// supremo tribunal de justiça
$feed = 'http://www.stj.jus.br/portal_stj/rss/index.wsp&#039;;

ini_set('allow_url_fopen', true);

$fp = fopen($feed, 'r');

$xml = '';

while (!feof($fp)) {

$xml .= fread($fp, 128);
}

fclose($fp);

$items = untag($xml, 'item');

$html = '<p>';

//foreach ($items as $item) {
//for($i=0;$i <= 5;$i++){
$count = 0;
foreach ($items as $item) {

if($count <= 4){

$title = untag($item, 'title');
$descricao = untag($item, 'description');
$link = untag($item, 'link');

$html .= '<a href="' . $link[0] . '">' . $title[0] . "</a><br />\n";


}
$count = $count + 1;
}

$html .= '</p>';

echo $html;



//----------------------------------------------------------------------------------------------------
//supremos tribunal federal
$feed = 'http://www.stf.jus.br/portal/RSS/noticiaRss.asp?codigo=1&#039;;

ini_set('allow_url_fopen', true);

$fp = fopen($feed, 'r');

$xml = '';

while (!feof($fp)) {

$xml .= fread($fp, 128);
}

fclose($fp);

$items = untag($xml, 'item');

$html = '<p>';

//foreach ($items as $item) {
//for($i=0;$i <= 5;$i++){
$count = 0;
foreach ($items as $item) {

if($count <= 4){

$title = untag($item, 'title');
$descricao = untag($item, 'description');
$link = untag($item, 'link');

$html .= '<a href="' . $link[0] . '">' . $title[0] . "</a><br />\n";


}
$count = $count + 1;
}

$html .= '</p>';

echo $html;


?>

Nova Resposta:

(Você pode usar tags como <b>, <i> ou <code>. URLs serão convertidas para links automaticamente.)