参考CDATA是另一个解析为PHP的XML网站上的URL

时间:2012-11-02 06:57:34

标签: php xml-parsing cdata

我有一个从其他网站导出的XML,其中包含CDATA,但当引用回网站时,此CDATA实际上是一个URL链接。我可以将此CData解析为PHP,但它会显示为文本。有没有办法可以将此CData链接回URL所在的网站?或者我需要在CData中解析URL而不是?

---来自其他网站---例如www.otherwebsite.com

例如来自XML的节点

<SongTitle><![CDATA[This is a song link at www.otherwebsite.com]]></SongTitle>

非常感谢。

1 个答案:

答案 0 :(得分:0)

将文件加载到字符串中时尝试preg_match_all ..

$string=  '<![CDATA[This is a song link at www.otherwebsite.com]]>'; // string of data or your xml file...

preg_match_all('/<!\[(CDATA)\[\s*(.*?)\s*\]\]>/',$string,$out);

$current_link = $out[2][0]; // print_r($_out); to see what is in this new array...
$current_link = str_replace("This is a song link at ","",$current_link); // now remove clutter
echo $current_link;

如果你想处理多次出现的CData ......

$var =  ' sdf sdf sdf<![CDATA[This is a song link at www.awesome.com]]> sdf sdf sd<![CDATA[This is a song link at www.google.com]]><![CDATA[This is a song link at www.amazon.com]]>';

preg_match_all('/<!\[(CDATA)\[\s*(.*?)\s*\]\]>/',$var,$out);
$array_of_links = $out[2];

foreach($array_of_links as $link){
    $current_link = str_replace("This is a song link at ","",$link);
    echo $current_link;
    echo "<br />";
}