2011-07-05 3 views
3

タグ間のデータを抽出するためのPHPのpreg_matchパターンを知りたい。例えばphp preg_matchタグを解析するためのパターン

<page> 
    <username>someone</username> 
    <id>3020778</id> 
    <text xml:space="preserve"> The quick brown fox. </text> 
</page> 

これは私の文字列 "速い茶色のキツネ" を提供します。

私は

preg_match('/<text(.*)?>(.*)?<\/text>/', $content, $match); 

を使用して試してみました。しかし、他のいくつかの例では動作しませんようです。

誰かがより良い解決策やパターンを持っていますか?

simpleXMLを使用すると、preg_matchよりも高速になりますか?

+2

通常、XML文書の解析は、正規表現を使用する場合よりも常に信頼性が高くなります。 – jerluc

+1

'$ page = simplexml_load_string($ xml); $ page-> id、$ page-> text; '[XMLファイルのノード値をCRUDするための簡単なプログラム]も参照してください(http://stackoverflow.com/questions/4906073/a-単純なプログラムからノー​​ドノードとノードの値のxmlファイル)、[Best XML Parser for PHP](http://stackoverflow.com/questions/188414/best-xml-parser-for -php/3616044#3616044)と[HTMLを解析するための最善の方法](0120)をご覧ください:http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662 – Gordon

+0

これは、 http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – tdammers

答えて

0
$a = '<page> 
<username>someone</username> 
    <id>3020778</id> 
    <text xml:space="preserve"> The quick brown fox. </text> 
</page>'; 

preg_match_all("(\<.+\>(.+)\<\/.+\>)U",$a, $r);  
?><pre><? print_r($r);?></pre><?