Jump to content

Returning text inside the xml elements


g_pmattoo

Recommended Posts

I have to write a PHP function that will take a string and a substring. The string is an xml like structure and i have to return the text that goes inside the xml element say <x> blah! </x>.

For example- if the string is <x>Hey!</x> <y>test </y> <z> Yup! </z> and the substring is x, then the function should return Hey!

 

Second thing is there could be multiple occurance of the same element. For example- <x>Hey!</x> <y>test </y> <z> Yup! </z> <x> second time! </x>, in that case the output should be the last text within the substring element i.e. second time! in the above example.

 

Any input will be highly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/86049-returning-text-inside-the-xml-elements/
Share on other sites

You can use regular expressions to match the appropriate tags, the php manual page for preg_match_all has an example you could use. If the string is valid XML then you can also use simpleXML and an XPATH expression to find the appropriate node.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.