Re: DOM Question. No pun intended.

From: Date: Fri, 15 Sep 2006 17:00:29 +0000
Subject: Re: DOM Question. No pun intended.
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-241832@lists.php.net to get a copy of this message
Crude outside-the-box work-around... You are only looking for <div and </div and what's between them, right? Playing with strpos and a simple stack for nested DIVs should let you hack this in a crude parser: <?php //Untested code, off the top of my head: $divs = array(); $stack = array(); $offset = 0; $len = strlen($html); while($offset < $len){ $divpos = strpos($html, '<div', $offset); $endpos = strpos($html, '</div', $offset); if ($divpos === false && $endpos === false) break; if ($divpos !== false && $divpos < $endpos){ array_push($stack, array($offset, $divpos)); $offset = $divpos + 1; } else{ $start = array_pop($stack); $divs = array($start, array($offset, $endpos)); $offset = $endpos + 1; } } var_dump($divs); ?> This should give you offsets to the beginning/end of each DIV tag. I'm sure I've got a one-off error or I'm not tracking quite the right numbers, or that I'm tracking extra numbers you don't need, but the idea is sound. -- Like Music? http://l-i-e.com/artists.htm

« previous php.general (#241832) next »