2017-04-18 3 views
0

こんにちは、私はlast.fmから情報を得ようとしていますが、アルバム名などを取得していますが、2行目から取得したいと思いますか?この行から取得する<topalbums artist = "Adele">

<topalbums artist="Adele" page="1" perPage="1" totalPages="55092" total="55092"> 

^ここから名、ページ、perpageと

<lfm status="ok"> 
<topalbums artist="Adele" page="1" perPage="1" totalPages="55092" total="55092"> 
<album> 

<name>21</name> 
<playcount>52308837</playcount> 
<mbid>c45e0e0e-48c9-4441-aac3-2f2b34202d3c</mbid> 
<url>https://www.last.fm/music/Adele/21</url> 
<artist> 

<name>Adele</name> 
<mbid>cc2c9c3c-b7bc-4b8b-84d8-4fbd8779e493</mbid> 
<url>https://www.last.fm/music/Adele</url> 
</artist> 
<image size="small"> 
https://lastfm-img2.akamaized.net/i/u/34s/c894af1e6a735b9bbb2a0312c7719f40.png 
</image> 
<image size="medium"> 
https://lastfm-img2.akamaized.net/i/u/64s/c894af1e6a735b9bbb2a0312c7719f40.png 
</image> 
<image size="large"> 
https://lastfm-img2.akamaized.net/i/u/174s/c894af1e6a735b9bbb2a0312c7719f40.png 
</image> 
<image size="extralarge"> 
https://lastfm-img2.akamaized.net/i/u/300x300/c894af1e6a735b9bbb2a0312c7719f40.png 
</image> 
</album> 
</topalbums> 
</lfm> 
をtotalpages
+1

早道コピー/貼り付けたりのDOMDocumentまたはSimpleXMLをを使用した...あなたは何を試してみましたか? – Gntem

+0

私は($ xmlを$ albumsとして)$ adele = albums-> topalbums-> adele –

答えて

0

Guysはfunnily私は最善の解決策

<?php 
$attrs = $xml7->topalbums->attributes(); 
echo $attrs["total"]; 
?> 
0

一つの基本的な方法は、正規表現を使用して、次のようになりますように(属性の順序場合

preg_match('/artist="([^"]*)"\\s*page="([^"]*)"\\s*perPage="([^"]*)"\\s*totalPages="([^"]*)"/', $lastfm_input, $result); 
# $result[1] = name value 
# $result[2] = page value 
# $result[3] = perPage value 
# $result[4] = totalPages value 

perPage)を変更すると、複数の正規表現を使用する必要があります。

+0

を試しました。いいえ、全く間違っています。正規表現はHTMLやXMLを解析するべきではありません。有名なStack Overflowの投稿があります。 – delboy1978uk

+0

私はそれがパーサーを使う方が良いということに同意します。私はこのオプションを提示したかっただけです。 (なぜなら、それを行う方法は常に複数あり、それは誰か他の人にも同様の問題を助けるかもしれないからです)あなたが言及した投稿へのリンクはありますか? –

+0

確かに - お楽しみください! :-D http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – delboy1978uk

1
$dom = new DOMDocument(); 
$dom->loadXML($xml); 
$topAlbums = $dom->getElementsByTagName('topalbums')->item(0); 
$artist = $topAlbums->getAttribute('artist'); 
echo $artist; // outputs Adele 
+0

のthatsの作業例を探しています。ありがとう –

関連する問題