PHP 自动套用段落格式标签

bug*_*com 2 html php autoformatting

这是一个相当基本的问题,但我只找到了三个答案。我想对可能包含也可能不包含其他 html 元素的未格式化内容进行自动段落标记。两个不错的功能是 wordpress wpautopHTMLPurifier AutoFormat.AutoParagraph和最后一些随机addParagraphsNew。然而,第一个与我的应用程序许可证不兼容,第二个不在段落标签内添加换行符,第三个是不稳定的。

有谁知道商业上合适的许可脚本,允许将具有换行符和双换行符的 html 内容转换为<br /><p>标签?

我相信 HTMLPurifier 选项可能是我侵入 AutoFormat.AutoParagraph 插件以使其添加换行符的最佳方法,但我希望能稍微简单一些。我知道很懒。

arn*_*rhs 5

好吧,你可以自己破解它(如果我正确理解你需要什么)

$text = "hello, I am text

and this is another paragraph, please do some cool
stuff with this (this is after a  line break)

last apragrahp...";

$text = str_replace("\r\n","\n",$text);

$paragraphs = preg_split("/[\n]{2,}/",$text);
foreach ($paragraphs as $key => $p) {
    $paragraphs[$key] = "<p>".str_replace("\n","<br />",$paragraphs[$key])."</p>";
}

$text = implode("", $paragraphs);

echo $text;
Run Code Online (Sandbox Code Playgroud)

这实际上会输出:

<p>hello, I am text</p><p>and this is another paragraph, please do some cool<br />stuff with this (this is after a  line break)</p><p>last apragrahp...</p>
Run Code Online (Sandbox Code Playgroud)

请注意它现在如何丢失所有换行符等......

  • 这没有考虑 html,它可能存在也可能不存在。如果 &lt;p&gt; 标签在 &lt;li&gt; 或 &lt;a&gt; 周围闭合,则生成的 html 将被破坏。 (3认同)