Jump to content

Archived

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

freshwebs

Parse Single HTML Tag

Recommended Posts

I need some help creating a function that will be passed a block of html, a search tag, and the instance.  It will find the end of that specific tag and return everything between its opening and closing tags.

For example:

[code]function get_tag_contents($haystack, $tag, $instance=0) {
  // function code
}[/code]

It would work like this:

[code]$html = "<div id="page"><div>text a</div><div>text b</div>text c<div><div>text d</div></div></div>";

get_tag_contents($html, "<div>");
// would return "<div>fasdfsadf</div><div>hjkh</div>asdfas<div><div>dfasdf</div></div>"

get_tag_contents($html, "<div>", 1);
// would return "text a"

get_tag_contents($html, "<div>", 3);
//would return "<div>text d</div>"[/code]

Can anyone help me with this?

Share this post


Link to post
Share on other sites
please redefine your query, i cant understand want to get from the e.g you have provided...

Share this post


Link to post
Share on other sites
the ereg family can do this trick for you, but parsing html, into objects is quite hard, especially with all the attributes and everything, if you have php 5, xml should be supported try looking into that code, maybe copy, past some of it, to get the function you are planning to use

Share this post


Link to post
Share on other sites
Yeah, I've thought about using the XML extensions but I don't have PHP5 installed on the server I use.

Share this post


Link to post
Share on other sites

×

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.