SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot phpguru's Avatar
    Join Date
    Sep 2005
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Match nested div with preg match

    i need to match all div / table block in the web page.
    i usually use

    Code:
    preg_match_all("/(.*)<(div|table)([^<>]*)>(.*)<\/(div|table)>(.*)/iUs",$s,$m);
    but this way went wrong with nested div

    Code:
    <div>a<div>b</div></div>
    ~> my code returns with the first </div>

    Code:
    <div>a<div>b</div>
    so, how can i get <div>a<div>b</div></div> (biggest div block) in every situation?

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Is there any reason why you want to use regular expressions? There are far more competent, easier to work with, robust tools available for working with HTML documents are you are.

    If you haven't already, and aren't restricted to using regex, have a look at the DOM. If that sounds like a good idea, or you want to stick with regex, let us know.
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    SitePoint Guru r2d2's Avatar
    Join Date
    Dec 2003
    Location
    In my van, fool!
    Posts
    646
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe look at using phpQuery - jQuery port to PHP ? Allows you to use jQuery style selectors - rather than lots of regex.

    PHP: DOM - Manual is also available, but I ended up with enormous amounts of code to do just simple things.


Bookmarks

Posting Permissions

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