tags:

views:

43

answers:

2

I have a string exactly like that:

<info>
<m_album>value1</m_album>     
<m_cat>value2</m_cat>     
<cat_type/>     
<cat_permission>0</cat_permission>     
<m_img_thumb><!--[CDATA[urlvaluetoimage]]--></m_img_thumb>     
<m_img_medium><!--[CDATA[urlvaluetoimage]]--></m_img_medium>     
<m_img_large><!--[CDATA[urlvaluetoimage]]--></m_img_large>     
<m_hot>0</m_hot>     
<album_hot>0</album_hot>     
</info>

How do I get value from these tags like XML in PHP? Please help me and thank you for your attention!

+3  A: 

You might want to look into the SimpleXML extension if you are using PHP5. Assuming the file above is loaded into a string called $xmlstr, you could access the value of m_album like this:

$xml = new SimpleXMLElement($xmlstr);
echo $xml->m_album;
Barnabas Kendall
omg! thank you very much Barnabas Kendall! I have had to waste a lot of time but I do not think of this function because I new in work with XML in PHP
gacon
+1  A: 
<m_img_thumb><!--[CDATA[urlvaluetoimage]]--></m_img_thumb>

is a comment, not any form of text content. If you really want to read the content of a comment you could eg. load it into a DOMDocument and:

$c= $doc->getElementsByTagName('m_img_thumb')[0]->firstChild->data;
$c= $c.substr(7, -2); // remove [CDATA[ and ]] from sides of string

But this is probably a mistake. Most likely you meant to use a CDATA section:

<m_img_thumb><![CDATA[urlvaluetoimage]]></m_img_thumb>

although there's not really much point in using CDATA sections compared to just normal text content. (CDATA sections are often mistakenly used by authors who think it stops them having to worry about XML-injection issues. Top tip: No.)

Either way, you can get the textual content of the element using eg.

$c= $doc->getElementsByTagName('m_img_thumb')[0]->textContent;
bobince
thank bobince your way may be complicated for me at this time! I will learning more of using CDATA tags in recent time!
gacon