Your question doesn't make sense to me unless you're talking about crawling a site and extracting content from a div on the page. RegEx's are powerful, but can be challenging to match in a lot of situations. Since ID's are supposed to be unique, we can use this to our advantage by searching for it (this WILL NOT WORK if the id is not unique):
PHP Code:
<?php $content = '<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"><!-- Makes HTML 5 elements available for styling --></script> </head> <body> <nav id="header_nav"> <ul> <li><a href=""></a></li> <li><a href=""></a></li> <li><a href=""></a></li> </ul> </nav> <div id="text1">My content</div> </body> </html>'; //Find id if (($id_pos = strpos($content, 'text1')) !== false) { $end_of_tag = strpos($content, '>', $id_pos); $close_tag = strpos($content, '</div>', $end_of_tag); echo substr($content, $end_of_tag + 1, $close_tag - $end_of_tag - 1); } ?>
@Matt: When writing a reg ex to apply to HTML tags, I usually use [^>]* for matching the inner contents as the > character should not show up (if done correctly) in the rest of a tag and will give non-greedy matches. When I use the dot character, I always end up playing with the ? greedy toggler to figure out if I have it right. Others more versed in RegEx's may have a different philosophy or easier way of understanding it, but I thought you may appreciate knowing someone else's way (with reasoning).
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
Last edited by JeremyMiller; 09-16-2009 at 01:55 PM..
|