2017-06-15 141 views
0

如何在保持原始單詞順序的同時解析與此處顯示的類似的xml?我的目標是僅提取第一個<span> -tag和<strong> -tag的內容,但詞語順序應保持不變(輸出應該是:國際清算銀行[BIZ])。我嘗試使用內置的php解析器(XML DOM和SimpleXML - Get),但是我無法保留單詞的順序。如何解析XML,同時在php中保留單詞的原始順序?

<span class="full_collocation"> 
    the<strong class="tilde">Bank</strong> for International Settlements 
</span> 
<span class="full_collocation"> 
    [<span class="or"><acronym title="or">or</acronym></span> BIZ] 
</span> 
+0

[如何剝離所有HTML標籤?(http://php.net/manual/en/function.strip-tags.php) – GiamPy

+0

我不想保持休息,在這種情況下,「或」 –

回答

2

使用DOMDocument,您應該能夠輕鬆獲得所需的值。看看這個例子:

$xmlString = '<root> 
    <span class="full_collocation"> 
    the<strong class="tilde">Bank</strong> for International Settlements 
    </span> 
    <span class="full_collocation"> 
    [<span class="or"><acronym title="or">or</acronym></span> BIZ] 
    </span> 
</root>'; 

$dom = new DOMDocument(); 
$dom->loadXML($xmlString); 
foreach($dom->documentElement->childNodes as $childNode) { 
    echo trim($childNode->textContent); // prints "theBank for International Settlements" and "[or BIZ]" 
} 
+0

這是我能夠實現的,然而在需要的過程中丟失了「銀行」這個詞。 –

+0

我更新了我的答案 – Adrien

+0

這非常有幫助,但有沒有辦法只輸出某些標籤的內容?在我的情況下,根標籤的直接子項和所有強標籤的span-tags? (我不想要「或」) –

相關問題