Page 1 of 1

xml content between tags

Posted: Sat Apr 16, 2005 11:03 am
by mcastles
Hi,

Is it possible to retrive the character data between tags as well as any tags that are in those tags?

for example, say I have..

Code: Select all

<html>
<head>
</head>
<body>
<table><tr><td>test</td></tr></table>
character data
</body>
and i want to retrieve everything between the body tags...

Code: Select all

<table><tr><td>test</td></tr></table>
character data
I can only seem to retrieve the character data of a tag.

Thanks

Posted: Sat Apr 16, 2005 11:06 am
by feyd
your examples aren't xml, but yes.. a regular expression can do that quite easily.

Check the Useful Posts thread (link in my signature) for several threads on using regex to extract data from a file/string. Other resources can be found by reading our stickies in this and potentially other boards.

Posted: Sat Apr 16, 2005 11:07 am
by Chris Corbyn
Yes...

Code: Select all

preg_match('/<body>(.*?)<\/body>/is', $string, $matches);
echo $matches[1];

Posted: Sat Apr 16, 2005 11:17 am
by mcastles
ahh thanks,

I was using xml parse functions etc.. using regular expressions is heaps simpler

Posted: Sat Apr 16, 2005 11:37 am
by timvw
btw, with the xml parsing....

you could would have to look at

function startElement($parser, $name, $attrs)

-> $name is the name of the tag
-> $attrs is an associative array (hash) with attributes

so <body bg="foo" at2="bar"></body> would result in a call
startElement($parser, "body" , array("bg" => "foo", "at2" => "bar"))


but if you are not sure the (x)html is not valid html, you are better of with regular expressions :)