0

I want to find content inside of a DIV. I just want to specify the DIV’s id; the rest is automatically adjusted under preg_match. E.g.:

<div id="midbuttonarea" etc...>some text...</div>
Gumbo
  • 643,351
  • 109
  • 780
  • 844
rajan
  • 1
  • 1
  • Is there something which prevents you from using phps DOM-classes? It would make things a lot easier. – Yoshi Apr 29 '11 at 07:17

2 Answers2

1

You're much more likely to get a good result using an HTML parser to parse HTML instead of Regex. It's extremely difficult to parse HTML with Regex, and the result may not be very reliable.

Check the accepted answer to this question: How do you parse and process HTML/XML in PHP? for some suggestions on how to go about it.

Community
  • 1
  • 1
Brenton Alker
  • 8,947
  • 3
  • 36
  • 37
1

Brenton is right. Anyway, here you have:

<?php
   function findDivInnerHtml($html, $id){
      preg_match("/<div .* id=\"{$id}\" .*>(.*)<\\/div>/i", $html, $matches);
      return $matches[1];
   }
?>

Sample usage:

<?php
   $html = '<div id="other"> xxx </div>  <div id="midbuttonarea" etc...>some text...</div> ';
   $innerHTML = findDivInnerHtml($html, 'midbuttonarea');       
   echo $innerHTML; //outputs "some text..."
?>

Hope this helps.

Adam Lynch
  • 3,341
  • 5
  • 36
  • 66
Edgar Villegas Alvarado
  • 18,204
  • 2
  • 42
  • 61