simpleXML get value from CDATA



I'm trying to get values from CDATA which are inside <b></b>. with simpleXML, but so far without any good results. Here is some part of my xml file -



<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://ift.tt/1qEgqrl
</link>
<pubDate>Thu, 25 Sep 2014 02:59:55 +0300</pubDate>
<description>
<![CDATA[
<a href="http://ift.tt/1qEgqrl"><img align=right border=0 src="http://ift.tt/1qEgqrm" 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://ift.tt/1qEgqrl">Apskatīt sludinājumu</a></b><br/><br/>
]]>
</description>
</item>


I know how to get values from this xml file like title, pudDate, link, but I don't know how can I get values from description tag so I can add them into database sorted by Price, District, Type, Image.


So far I tryed to save description tag into string and after that using explode() cut out parts with info I need, I have the right values but they come with tags. Some with tags.


This is what I was trying -



$url = "http://ift.tt/1qEgoQd";

$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];
}


but I think my solution is absoloutly wrong and the result of cutting is - <b><b>300 or <b>650


so my question is: how can I get clean values out of my CDATA using something similar to



$pubDate = $item->pubDate


using something like that?


$description = (string)$item->description->b[0] - to get right values from CDATA.


No comments:

Post a Comment