Regex 使用php从MySQL到HTML的文本-段落格式(br到p)preg_替换

Regex 使用php从MySQL到HTML的文本-段落格式(br到p)preg_替换,regex,preg-replace,Regex,Preg Replace,MySQL中有文本。 使用php,我将其从DB提取到变量“text”,并执行以下格式化: $text = nl2br($text); // use php function to add br tags for new lines in DB $text = str_replace("<br />", "</p><p>", $text); // replace br tags with opening and closi

MySQL中有文本。 使用php,我将其从DB提取到变量“text”,并执行以下格式化:

$text = nl2br($text); // use php function to add br tags for new lines in DB
$text = str_replace("<br />", "</p><p>", $text); // replace br tags with opening and closing p tag
$text = "<p>" . $text . "</p>"; // add opening and closing tags to the beginning and end of text
$pattern = '/<\/(div|ol|ul|details|summary)>[\W]*<\/p><p>/i';
$text = preg_replace($pattern, '</$1><p>', $text);
$text=nl2br($text);//使用php函数为DB中的新行添加br标记
$text=str_replace(“
”,“

”,$text);//用开始和结束p标记替换br标记 $text=“”$文本。“

”;//将开始和结束标记添加到文本的开头和结尾 $pattern='/[\W]*/i'; $text=preg_replace($pattern,,$text);
在HTML验证中,我遇到了一个错误:

。。。 我用不同的方式玩花样:

...summary)>[\s]*<\/p>...
...summary)>[\W\s]*<\/p>...
...summary)>\s*<\/p>...
...summary)>\W*<\/p>...
…摘要)>[\s]*。。。
…摘要)>[\W\s]*。。。
…摘要)>\s*。。。
…摘要)>\W*。。。
没有人在工作。 不知怎的,那个部分没有匹配

以下是我对php文件的测试/调试代码:

$str = '<ol><li>list 1</li><li>list 2.</li></ol> </p><p>
test begin.</p>
<details class="intext"><summary>summary</summary></p><p>
test words in details</p>
</details> </p><p>
<div>test1</div> </p><h2>Test1</h2>
<div>test1.2</div> </p><h2>Test1.2</h2>
<div>test2</div>test3</p><h2>Test2</h2>
<div>test3</div></p><h2>Test2</h2>
<div>test4</div>
</p><h2>Test4</h2>
';

$pattern = '/<\/(div|ol|ul|details|summary)>[\W]*<\/p><p>/i';
$newText = preg_replace($pattern, '</$1><p>', $str);

$newText2string = htmlspecialchars($newText);
echo "<pre>$newText2string</pre>";
echo "<hr>" . $newText;
$str='
  • 列表1
  • 列表2.
  • 测试开始

    摘要

    详细测试单词

    test1

    test1 test1.2

    test1.2 test2test3

    Test2 测试3

    Test2 测试4

    Test4 '; $pattern='/[\W]*/i'; $newText=preg_replace($pattern,,$str); $newText2string=htmlspecialchars($newText); 回显“$newText2string”; 回声“
    ”$新文本;
    它适用于所有其他标记,但
    。。。 我没有看到什么