0

I have the following text:

<div id="parent">
      <div class="box1"> content 1</div>
      <div class="box2"> content 1</div>
      <div class="box3"> content 1 <div class="box31"></div></div>
</div>

What I've tried:

preg_match_all ("/<div id=\"parent\">([^`]*?)<\/div>/", $str_test, $matches);
print_r($matches);exit;

I want to get all the content:

<div class="box1"> content 1</div>
<div class="box2"> content 1</div>
<div class="box3"> content 1 <div class="box31"></div></div>

It is not working and I need help.

Federkun
  • 36,084
  • 8
  • 78
  • 90

2 Answers2

1

You shouldn't parse HTML with regex. You can and you should do that with DOMDocument.

$string = '<div id="parent">
      <div class="box1"> content 1</div>
      <div class="box2"> content 1</div>
      <div class="box3"> content 1 <div class="box31"></div></div>
</div>';

$dom = new DOMDocument();
$dom->loadHTML($string);

$xpath = new DOMXPath($dom);
$parentNode = $xpath->query("//div[@id='parent']");

$html = '';
foreach ($parentNode->item(0)->childNodes as $node) {
    $html .= $node->ownerDocument->saveHtml($node);
}

echo $html;

The saveHTML method allows you to get the whole html from each node of the node parent (<div id="parent">).

Demo.

lepe
  • 24,677
  • 9
  • 99
  • 108
Federkun
  • 36,084
  • 8
  • 78
  • 90
0

If you really need to use regex, perhaps as a general text parsing not limited to well-formed HTML, then you should:

  1. Use preg_match instead of preg_match_all

  2. Lose the ? in your pattern.

Your desired result should be inside $matches[1].

light
  • 4,157
  • 3
  • 25
  • 38