【发布时间】:2014-11-22 02:02:14
【问题描述】:
我正在尝试从 <b></b> 中的 CDATA 获取值。用simpleXML,但到目前为止还没有任何好的结果。这是我的 xml 文件的一部分 -
<item>
<title>
<![CDATA[
Bez starpniekiem tiek izīrēts pilnībā mēbelēts 1-istabu dzīvoklis 5. stāvā uz ilgu laiku. Dzīvoklis mēbelēts, ar iebūvētu vir ...
]]>
</title>
<link>
http://www.ss.lv/msg/lv/real-estate/flats/riga/centre/abhkp.html
</link>
<pubDate>Thu, 25 Sep 2014 02:59:55 +0300</pubDate>
<description>
<![CDATA[
<a href="http://www.ss.lv/msg/lv/real-estate/flats/riga/centre/abhkp.html"><img align=right border=0 src="http://i.ss.lv/images/2014-09-24/348773/VHkBG09gR1s=/1.t.jpg" width="160" height="120" alt=""></a>
District: <b><b>centrs</b></b><br/>Street: <b><b>Klijānu 2</b></b><br/>Rooms: <b><b>1</b></b><br/>m2: <b><b>35.00</b></b><br/>Type: <b><b>Renov.</b></b><br/>: <b><b>8.57</b> €</b><br/>Price: <b><b>300</b> €/mēn.</b><br/><br/><b><a href="http://www.ss.lv/msg/lv/real-estate/flats/riga/centre/abhkp.html">Apskatīt sludinājumu</a></b><br/><br/>
]]>
</description>
</item>
我知道如何从这个 xml 文件中获取值,例如标题、pudDate、链接,但我不知道如何从描述标签中获取值,以便我可以将它们添加到按价格、地区、类型、图像排序的数据库中.
到目前为止,我尝试将描述标签保存到字符串中,然后使用explode() 剪切带有我需要的信息的部分,我有正确的值,但它们带有标签。有些带有标签。
这就是我正在尝试的 -
$url = "http://www.ss.lv/lv/real-estate/flats/riga/hand_over/rss/";
$result = simplexml_load_file($url);
foreach ($result->channel->item as $item) {
$title =(string)$item->title;
description = (string)$item->description;
$link = $item->link;
$pubDate = $item->pubDate;
// Cut out from description price
$parts = explode("Price: ", $description);
$pri= "";
for ($i = 1; $i < 2; $i++) {
$pri= $parts[$i];
}
$parts2 = explode("</b>", $pri);
for ($i = 1; $i < 2; $i++) {
$price= $parts2[0];
}
但我认为我的解决方案是绝对错误的,切割的结果是 - <b><b>300 or <b>650
所以我的问题是:如何使用类似于
的方法从 CDATA 中获取干净的值$pubDate = $item->pubDate
使用类似的东西?
$description = (string)$item->description->b[0] - 从 CDATA 中获取正确的值。
【问题讨论】:
标签: php xml xml-parsing simplexml cdata