【发布时间】:2019-07-09 06:15:31
【问题描述】:
我试图在网站上显示一个干净的段落,但是由于内容中间的<p></p>,我没有得到预期的输出。我尝试了以下方法,但都没有奏效,我的内容仍然是<p></p>。
$story = "<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>";
1. $story = strip_tags($story, '<p>');
2. $story=str_ireplace('<p>','',$story);
3. $story=str_ireplace('</p>','',$story);
4. $story = preg_replace('/<p\b[^>]*>(.*?)<\/p>/i', '', $story);
我是否遗漏了代码中的任何内容?我的预期输出是
答案:自 1500 年代以来,Lorem Ipsum 一直是行业的标准虚拟文本,当时一位不知名的印刷商采用了一种类型的厨房并将其加扰以制作一本类型样本书。自 1500 年代以来,Lorem Ipsum 一直是行业的标准虚拟文本,当时一位不知名的印刷商使用了一种类型的厨房并加扰它来制作类型样本书。
【问题讨论】:
-
$story 是否只包含
<p>html 标记而没有其他HTML 标记? -
如果您将
<p>作为第二个参数传递给strip_tags(),您是在告诉函数允许该标签,而不是删除它。如果要去除所有标签,只需省略第二个参数。如果您想保留其他 html 标记,则应将其作为第二个参数传递。 Here's the manual -
你的预期输出是什么?
-
@vivek_23 我在内容中有
<img>标签,但现在,我只想从内容中删除<p>标签($story)。
标签: php preg-replace strip-tags