Jump to content

Parse Single HTML Tag


freshwebs

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?
Link to comment
https://forums.phpfreaks.com/topic/15958-parse-single-html-tag/
Share on other sites

  • 2 weeks later...
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
Link to comment
https://forums.phpfreaks.com/topic/15958-parse-single-html-tag/#findComment-70297
Share on other sites

Archived

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

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