去掉 来自HTML内容的标记

问题描述 投票:1回答:4

我试图在网站上显示一个干净的段落,但由于内容中间的<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);

我在代码中遗漏了什么吗?我的预期产量是

答案:自16世纪以来,Lorem Ipsum一直是业界标准的虚拟文本,当时一个未知的打印机拿了一个类型的厨房,并拼写它制作一个类型的样本书。自16世纪以来,Lorem Ipsum一直是业界标准的虚拟文本,当时一台未知的打印机采用了类型的厨房,并将其拼凑成一本类型的样本。

php preg-replace strip-tags
4个回答
1
投票

str_replace()尝试这种方式

<?php
$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>";
$result = str_replace(['<p>','</p>'],['',''],$story);
echo $result;
?>

但是:ku zxsw指出

或者使用https://3v4l.org/nllPP删除所有标记,顺便说一句,您可以使用此方法指定allowable_tags

strip_tags()

2
投票

PHP具有内置函数来从字符串中删除HTML标记。最好使用$result = strip_tags($story); 而不是strip_tags(),这是一个例子

str_replace()

1
投票

使用 $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>"; ; $a = strip_tags($story); $b = strip_tags($story, "<strong><em>"); // can also pass the tags that you don't want to remove 函数。

str_ireplace()

但是:Kua zxsw指出


1
投票
$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>";

echo $text=str_ireplace('<p>','',$story);

有一个内置函数,删除php中的所有html标签。用strip_tags()

© www.soinside.com 2019 - 2024. All rights reserved.