从HTML内容中删除<p>标签

时间:2019-02-15 06:18:59

标签: php preg-replace strip-tags

我试图在网站上显示干净的段落,但是由于内容中间的<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一直是行业的标准伪文本,当时一位不知名的打印机拿来一个样板间,然后将其拼写成样本。

4 个答案:

答案 0 :(得分:2)

PHP具有内置功能,可从字符串中删除HTML标记。最好使用flutter doctor -v 而不是strip_tags(),这是一个示例

str_replace()

答案 1 :(得分:2)

使用str_ireplace()函数。

$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);

演示:https://3v4l.org/W0Qeb

答案 2 :(得分: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;
?>

演示https://3v4l.org/nllPP

strip_tags()一起删除所有标签,通过这种方法,您可以指定 allowable_tags

$result = strip_tags($story);

答案 3 :(得分: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>";

$story = strip_tags($story) ;
echo $story;

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