XMLデータ内のタグ件数と属性値をループ取得-PHP simplexml_load_file

PHPのsimplexml_load_fileでXMLをパースし、タグ内の属性値、
例えば

<item title=”かっぱ”>

のような場合にtitle属性の”かっぱ”にあたる部分を、7つ取得する。
7件以下の場合は、7件以下の件数分のtitle属性値を取得して表示する場合の記述

<? php
$rssdata = simplexml_load_file(‘http://***.com’);
$arrayCnt = array();
$tcount;
foreach($rssdata -> channel ->item as $acount){
array_push($arrayCnt, (string)$acount);
$rcount = count($arrayCnt);
if($rcnt > 7){
$tcount = 7;
break;
}
}
$tcount = $rcount;
for ($i=0; $i<$tcount; $i++){ print "<h3 class='lcnt-ssec-ttl'>"; print "<div>"; print $rssdata0 -> channel ->item[$i][‘title’];
print “</div>”;

}
?>

コメントを残す

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください