$re = '/<([^\/>]+)>((([^\<]+)|(?R))*)<\/\1>/mx';
$str = '<ul><li><span>qwe</span><b></b></li><div>sometext</div></ul><div></div>.
<li><span>qwe</span><div>s</div></li>
<b></b>
<ul><li><span>qwe</span><b></b></li><div>sometext</div></ul><div></div>.
some text <tag>some|another|text</tag> and anything else
str = <<\'EOT\'
Это <!--пример текста--> с5
<a href="#">какой-то</a> <br>разметкой <br />
<b class="11">и</b> <amg src="1">
<area>непарными</area> <strong>
тегами</strong> и, <!--коментами-->,
<ul id="w"> <li id="id" class="class">1</li></ul><div></div>.
<u class="ну и даже так" />
EOT';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php