0

I'm looking for a way to find a html div with a certain ID using php

<?php
    $regex = "<div+[a-zA-Z0-9._-\"]+id=\"";
    $string = '<html><body><div style="rubbish" id="man"></body></html>';
    preg_match($regex, $string, $matches, PREG_OFFSET_CAPTURE);
    $var_export = $matches;
    $var = $var_export[1][1];
    echo substr($string, $var, 3);
?>

I know this is a load of rubbish at the momment but I can't quite get my head around regular expressions.

Marc
  • 3,683
  • 8
  • 34
  • 48
user253379
  • 11
  • 1
  • 1
    **Don't use regular expressions to parse HTML. Use a proper HTML parsing module.** You cannot reliably parse HTML with regular expressions, and you will face sorrow and frustration down the road. As soon as the HTML changes from your expectations, your code will be broken. See http://htmlparsing.com/php or [this SO thread](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) for examples of how to properly parse HTML with PHP modules that have already been written, tested and debugged. – Andy Lester May 02 '14 at 16:22
  • What are you trying to match and what's the expected result? – Amal Murali May 02 '14 at 16:28
  • There's no closing tag for div `man`, is this right ? – Pedro Lobito May 02 '14 at 16:32

1 Answers1

0

You may want to try this:

$html = '<html><body><div style="rubbish" id="man">something </div><div id="otherid">blabla</div></body></html>';

preg_match_all('%(<div.*?id="man">.*?</div>)%im', $html, $result, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($result[1]); $i++) {
    echo $result[1][$i];
}

DEMO

http://ideone.com/KQv3OA

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268