0

これに関する他のいくつかのSOの投稿を見ましたが、喜びはありません。

私はこのコードを持っています:

$url = "http://itunes.apple.com/us/rss/toppaidapplications/limit=10/genre=6014/xml";
$string = file_get_contents($url);
$string = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $string);
$xml = simplexml_load_string($string);

foreach ($xml->entry as $val) {
    echo "RESULTS: " . $val->attributes() . "\n";

しかし、私は結果を得ることができません。このフラグメントで 549592189 になる ID 値を取得することに特に関心があります。

<id im:id="549592189" im:bundleId="com.activision.wipeout">http://itunes.apple.com/us/app/wipeout/id549592189?mt=8&amp;uo=2</id>

助言がありますか?

4

3 に答える 3

0

で試してくださいxpath

$doc     = new DOMDocument;
@$doc->loadHTML($string);
$xpath   = new DOMXpath($doc);
$r       = $xpath->query("//id/@im:id");
$id      = $r->item(0)->value;
于 2012-09-07T22:05:14.177 に答える
0

SimpleXML を使用すると、XML 構造をドリルダウンして必要な要素を簡単に取得できます。それが何をするにしても、正規表現は必要ありません。

<?php

// Load XML
$url = "http://itunes.apple.com/us/rss/toppaidapplications/limit=10/genre=6014/xml";
$string = file_get_contents($url);
$xml = new SimpleXMLElement($string);

// Get the entries
$entries = $xml->entry;

foreach($entries as $e){
    // Get each entriy's id
    $id = $e->id;
    // Get the attributes
    // ID is in the "im" namespace
    $attr = $id->attributes('im', TRUE);
    // echo id
    echo $attr['id'].'<br/>';
}

デモ: http://codepad.viper-7.com/qNo7gs

于 2012-09-07T22:11:57.880 に答える