Take the 2-minute tour ×

I'm looking for a way to find a html div with a certain ID using 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.

share|improve this question
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 htmlparsing.com/php or this SO thread for examples of how to properly parse HTML with PHP modules that have already been written, tested and debugged. –  Andy Lester May 2 at 16:22
What are you trying to match and what's the expected result? –  Amal Murali May 2 at 16:28
There's no closing tag for div man, is this right ? –  Pedro Lobito May 2 at 16:32

1 Answer 1

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];



share|improve this answer

Your Answer


