www.webdeveloper.com
Results 1 to 7 of 7

Thread: Find contents of an HTML Tag

  1. #1
    Join Date
    Dec 2006
    Posts
    46

    Find contents of an HTML Tag

    Hey, I need to be able to get the contents of the content between 2 matching (IE opening and closing) HTML tags. That's the easy part...
    The hard part is PHP will need to figure out which closing tag matches its opening.
    So PHP would get this:
    Code:
    $input = '
    BoringContentBoringContentBoringContentBoringContent
    <div class='hello you'> 
    <h4>Title</h4>
    <div class='yet another div tag'>
    Content inside this div
    </div>
    MoreBoringContentMoreBoringContent
    </div>
    BoringContentBoringContent
    ';
    and PHP would output this:
    Code:
    <h4>Title</h4>
    <div class='yet another div tag'>
    Content inside this div
    </div>
    MoreBoringContentMoreBoringContent
    Any ideas? This is a pretty complicated one.
    Thanks,
    Brendan
    Last edited by The_Wanderer; 01-17-2007 at 01:16 PM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    You might be able to use the Tidy extension to do this.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Regex is no good for this as it does have a mechanism to deal with recurssion. Probably, the best way is to build a stack.

  4. #4
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    You need a parser. Perhaps use Tidy to transform the HTML to XHTML and then parse it as XML.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  5. #5
    Join Date
    Dec 2006
    Posts
    46
    I don't know if Tidy could do this, any other suggestions?

  6. #6
    Join Date
    Nov 2006
    Posts
    73
    Well would that really be as complicated to code?
    You could just search for all divs and look if the class is the one you want.
    After this you look for div close tag. If there is an open-div tag between the one with your class and the close tag you will have to go on searchin, if there isn't youve got your text!

  7. #7
    Join Date
    Dec 2006
    Posts
    46
    That sounds like a good idea! Thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles