- preg_match_all: documentation ( source)
- var_export: documentation ( source)
<?php
$html = <<<HTML
<div>
<h1>title</h1> and <h2>title 2</h2>
<h2 data-formula="a > b" class="regex_is_DOM_unaware">Monkeywrench</h2>
<h1 class="classname">test <span>This isn't even a </h tag</span></h1>
</div>
HTML;
var_export(
preg_match_all(
'|<\s*h[1-2](?:.*)>(.*)</\s*h|Ui',
$html,
$matches
)
? $matches[1] // capture group 1
: []
);
/* Desired result but not what is provided:
array (
0 => 'title',
1 => 'title 2',
2 => 'Monkeywrench',
3 => 'test <span>This isn't even a </h tag</span>'
)
*/