0

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!

2 Answers 2

4

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;
Sign up to request clarification or add additional context in comments.

1 Comment

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
1
<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;

1 Comment

thank bobince your way may be complicated for me at this time! I will learning more of using CDATA tags in recent time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.