【问题标题】:How can i get the post content without html tags?如何获取没有 html 标签的帖子内容?
【发布时间】:2013-03-20 17:25:01
【问题描述】:

我正在使用JSON API 并希望解析没有 html 标记的帖子内容(明文)。

我尝试使用 htmlspecialchars(json_encode($posts))strip_tags($posts); 但无法从 JSON 中删除 html 标签。

不确定我是否正确放置它,因为我是 php 新手。

public function get_category_posts() {
global $json_api;
$category = $json_api->introspector->get_current_category();
if (!$category) {
  $json_api->error("Not found.");
}
$posts = $json_api->introspector->get_posts(array(
  'cat' => $category->id
));
$result = strip_tags($posts);
return $this->posts_object_result($result, $category);
}

JSON

"posts": [
{
  "id": 3454,
  "type": "post",
  "status": "publish",
  "title": "XYZ JOINS",
  "content": "<p>This is the content that should not have html tags.<\/p>\n",
  "date": "2012-05-16 22:06:55"
}
]

我想从上面的 json 中删除 &lt;p&gt;&lt;/p&gt; html 标签。内容中有许多 div 和其他 html 标签。

【问题讨论】:

  • 您能否提供一些示例输入/输出,说明问题所在以及您希望它的外观,因为目前很难说出您想要什么
  • 谢谢!我修改了问题以获得更多说明。
  • strip_tags 仅适用于字符串。也许您应该将此功能仅用于Content 而不是整个帖子?
  • @Philipp 你能举个例子吗?

标签: php html json wordpress


【解决方案1】:

我认为您只需要一个简单的正则表达式即可从内容中去除 html 样式标签。也许这会起作用

str.replace(/<\/?[^>]+>/gi, '')

【讨论】:

  • 我是否需要循环访问所有 $posts,不知道如何在 php 上执行此操作
  • 取决于您的 php 代码实现。如果您一次又一次地获取数据,那么是的
  • str.replace 中的 gi 是什么?
  • 它是替换字符串类型的规范设置
猜你喜欢
  • 2015-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多