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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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