g_pmattoo Posted January 15, 2008 Share Posted January 15, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/86049-returning-text-inside-the-xml-elements/ Share on other sites More sharing options...
KrisNz Posted January 15, 2008 Share Posted January 15, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/86049-returning-text-inside-the-xml-elements/#findComment-439449 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.