<?php
/*
Хочу получить
<div class="block">
<h1>Первая строчка</h1>
<ul>
<li>Вторая строчка</li>
<li>Третяя строчка</li>
</ul>
</div>
*/
$text = <<<HTML
<ul>
<li>Первая строчка</li>
<li>Вторая строчка</li>
<li>Третяя строчка</li>
</ul>
HTML;
$text = preg_replace("/\<ul\>\n\<li\>(.*?)\<\/li\>/", "/\<h1\>(.*?)\<\/h1\>\n\<ul\>/", $text); $text = str_replace("<h1>", "<div class='block'><h1>", $text); echo $text;
PD9waHAKLyogCgnQpdC+0YfRgyDQv9C+0LvRg9GH0LjRgtGMCgkKPGRpdiBjbGFzcz0iYmxvY2siPgo8aDE+0J/QtdGA0LLQsNGPINGB0YLRgNC+0YfQutCwPC9oMT4KPHVsPgo8bGk+0JLRgtC+0YDQsNGPINGB0YLRgNC+0YfQutCwPC9saT4KPGxpPtCi0YDQtdGC0Y/RjyDRgdGC0YDQvtGH0LrQsDwvbGk+CjwvdWw+CjwvZGl2PgoqLwoKJHRleHQgPSA8PDxIVE1MCjx1bD4KPGxpPtCf0LXRgNCy0LDRjyDRgdGC0YDQvtGH0LrQsDwvbGk+CjxsaT7QktGC0L7RgNCw0Y8g0YHRgtGA0L7Rh9C60LA8L2xpPgo8bGk+0KLRgNC10YLRj9GPINGB0YLRgNC+0YfQutCwPC9saT4KPC91bD4KSFRNTDsKCiR0ZXh0ID0gcHJlZ19yZXBsYWNlKCIvXDx1bFw+XG5cPGxpXD4oLio/KVw8XC9saVw+LyIsICIvXDxoMVw+KC4qPylcPFwvaDFcPlxuXDx1bFw+LyIsICR0ZXh0KTsKJHRleHQgPSBzdHJfcmVwbGFjZSgiPGgxPiIsICI8ZGl2IGNsYXNzPSdibG9jayc+PGgxPiIsICR0ZXh0KTsKJHRleHQgPSBzdHJfcmVwbGFjZSgiPC91bD4iLCAiPC91bD48L2Rpdj4iLCAkdGV4dCk7CmVjaG8gJHRleHQ7Cg==