我有这样的HTML代码:
<li class="recipe-ingredient">
<span class="recipe-ingredient-quantity-unit">
<span data-original="" data-fraction="" data-normalized="0" class="recipe-ingredient-quantity recipe-ingredient-quantity"></span>
<span data-original="" class="recipe-ingredient-unit recipe-ingredient-unit"></span>
</span>
<span class="recipe-ingredient-name recipe-ingredient-name"><a href="/ingredient/water/">water</a></span>
<span class="recipe-ingredient-notes recipe-ingredient-notes">For Kneading</span>
</li>
使用preg_replace
,我希望将第一组<span>
包含在一个<div>
中,将最后两组<span>
包含在另一个<div>
中,这样我的最终版本结果将是:
<li class="recipe-ingredient">
<div class="ing-qt-unit">
<span class="recipe-ingredient-quantity-unit">
<span data-original="" data-fraction="" data-normalized="0" class="recipe-ingredient-quantity recipe-ingredient-quantity"></span>
<span data-original="" class="recipe-ingredient-unit recipe-ingredient-unit"></span>
</span>
</div>
<div class="ing-name-notes">
<span class="recipe-ingredient-name recipe-ingredient-name"><a href="/ingredient/water/">water</a></span>
<span class="recipe-ingredient-notes recipe-ingredient-notes">For Kneading</span>
</div>
</li>
答案 0 :(得分:0)
第一个应该赶上第一组:
<span class[^>]+>(?:[^<]+<[^>]+>[^<]*<[^>]+>)*[^<]*</span>
这个应该赶上第二集:
<span class[^>]+>(?:[^<]*<[^>]+>[^<]+<[^>]+>)*[^<]*</span>
像这样使用它以避免转义:
$re = "@<span class[^>]+>(?:[^<]*<[^>]+>[^<]+<[^>]+>)*[^<]*</span>@im";
替换应该类似于"<div class="ing-name-notes">$0</div>"
。