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>
1
  • Is there something which prevents you from using phps DOM-classes? It would make things a lot easier. Commented Apr 29, 2011 at 7:17

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Forgot to tell you that $html doesn't have to have line breaks. You can easily remove them with str_replace first
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.

1 Comment

it can be possible in php regular expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.