Jump to content


Photo

Parse Single HTML Tag


  • Please log in to reply
4 replies to this topic

#1 freshwebs

freshwebs
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 29 July 2006 - 04:59 PM

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:

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

It would work like this:

$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>"

Can anyone help me with this?

#2 freshwebs

freshwebs
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 06 August 2006 - 05:32 PM

Any ideas?

#3 xec

xec
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 06 August 2006 - 05:36 PM

please redefine your query, i cant understand want to get from the e.g you have provided...

#4 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,429 posts
  • LocationBelgium

Posted 06 August 2006 - 05:39 PM

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

#5 freshwebs

freshwebs
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 06 August 2006 - 05:44 PM

Yeah, I've thought about using the XML extensions but I don't have PHP5 installed on the server I use.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users