xml content between tags

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcastles
Forum Newbie
Posts: 8
Joined: Thu Oct 21, 2004 6:54 pm

xml content between tags

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yes...

Code: Select all

preg_match('/<body>(.*?)<\/body>/is', $string, $matches);
echo $matches[1];
mcastles
Forum Newbie
Posts: 8
Joined: Thu Oct 21, 2004 6:54 pm

Post by mcastles »

ahh thanks,

I was using xml parse functions etc.. using regular expressions is heaps simpler
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 :)
Post Reply