正则表达式标记之间的新行

时间:2012-07-04 12:04:05

标签: php regex

我有一个带有正则表达式的php代码,它在li标签中的p标签中的新行上失败。当我执行此代码时,它会从中删除</ul></li>

<?php
$data   =   "<h1>test</h1>
            <h2>test</h2>
            <p>This is not real text but just a test, This is not real text but just a test, This is not real text but just a test, This is not real text but just a test, This is not real text but just a test</p>
            <ul><li><p>This is not real text but just a test, This is not real text but just a test, This is not real text but just a test, This is not real text but just a test</p>
            </li></ul>
            <ul><li><p>This is not real text but just a test, This is not real text but just a test, This is not real text but just a test, This is not real text but just a test</p>
            </li></ul>";

$pattern    =   "#[\<ul\>\<li\>]*\<[p]*[h1]*[h2]*[h3]*\>(.+?)\</[p]*[h1]*[h2]*[h3]*\>[\</li\>\</ul\>]*#is";
preg_match_all($pattern, $data, $output);
var_dump($output);
?>

任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

我不完全确定你想要实现的目标,但我认为纠正模式会是这样的:

#\s*(<ul><li>)?(\s*<(p|h1|h2|h3)>)*(.+?)(</(p|h1|h2|h3)>\s*)*(</li></ul>)?#is

编辑:纠正了一个错误