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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.