在 php 中用换行符 (\r\n\r\n) 替换段落 HTML 元素

发布于 2025-01-11 15:34:10 字数 405 浏览 0 评论 0原文

这里的大多数答案都是关于将换行符转换为

但我想做相反的事情。我需要

\r\n\r\n

因为 WordPress 的 WCFM 市场插件强制使用 HTML 元素,但我需要将描述输出为 JSON 对象,如下所示,而不使用 HTML。

{
    "name": "Hijo",
    "description": "<p>Too cute to avoid. You'd so wanna bring her home.</p><p>Each of our pets is .....",
}

或者是否有与 wpaautop() 相反的函数?

Most answers here are about converting line breaks to <p> but I'm looking to do the opposite. I need <p> to be \r\n\r\n.

Because the WCFM marketplace plugin for WordPress is forcing the use of HTML elements but I need to output my description as JSON object as below, without HTML.

{
    "name": "Hijo",
    "description": "<p>Too cute to avoid. You'd so wanna bring her home.</p><p>Each of our pets is .....",
}

Or is there an opposite function to wpautop()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝不服输 2025-01-18 15:34:10

您可以使用 strtr 来交换字符串。

$description = "<p>Too cute to avoid. You'd so wanna bring her home.</p><p>Each of our pets is </p><p>3rd Line.....";

$descriptionLf = strtr($description,['<p>' => '','</p>' => "\r\n\r\n"]);

echo '<pre>'.$descriptionLf.'</pre>';

注意: \n 等特殊字符必须用双引号引起来。

输出:

Too cute to avoid. You'd so wanna bring her home.

Each of our pets is 

3rd Line.....

You can just use strtr to swap the strings.

$description = "<p>Too cute to avoid. You'd so wanna bring her home.</p><p>Each of our pets is </p><p>3rd Line.....";

$descriptionLf = strtr($description,['<p>' => '','</p>' => "\r\n\r\n"]);

echo '<pre>'.$descriptionLf.'</pre>';

Note: The special characters like \n must be in double quotes.

Output:

Too cute to avoid. You'd so wanna bring her home.

Each of our pets is 

3rd Line.....
鸩远一方 2025-01-18 15:34:10

您可以使用 preg_match 函数查找

并将其替换为 \r\n(带有空格的

you can use preg_match function to find and replace the <p> with \r\n, an </p> with blank space

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文