【问题标题】:Remove <p> tag from HTML content从 HTML 内容中删除 <p> 标记
【发布时间】:2019-07-09 06:15:31
【问题描述】:

我试图在网站上显示一个干净的段落,但是由于内容中间的&lt;p&gt;&lt;/p&gt;,我没有得到预期的输出。我尝试了以下方法,但都没有奏效,我的内容仍然是&lt;p&gt;&lt;/p&gt;。

$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 是否只包含&lt;p&gt; html 标记而没有其他HTML 标记?
  • 如果您将&lt;p&gt; 作为第二个参数传递给strip_tags(),您是在告诉函数允许该标签,而不是删除它。如果要去除所有标签,只需省略第二个参数。如果您想保留其他 html 标记,则应将其作为第二个参数传递。 Here's the manual
  • 你的预期输出是什么?
  • @vivek_23 我在内容中有&lt;img&gt; 标签,但现在,我只想从内容中删除&lt;p&gt; 标签($story)。

标签: php preg-replace strip-tags


【解决方案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

OR 与strip_tags() 删除所有标签,顺便用这种方法你可以指定allowable_tags

$result = strip_tags($story);

【讨论】:

  • 我看到了运行良好的演示,但我的代码中没有得到相同的输出。我在变量 $story 中获取 Unicode 字符,这会导致我出现问题吗?
  • 您给出的每个答案都在这里完美运行:3v4l.org/nllPP 但不在我的代码中。
  • 我看了看那里,但没有用。而且,是的,我尝试在循环之外使用 Unicode 字符,但我在 foreach 循环中显示 $story 并且不起作用。
【解决方案2】:

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

     $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

【讨论】:

    【解决方案3】:

    使用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

    【讨论】:

      【解决方案4】:
      $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.ini 中的所有 html 标签。 strip_tags()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-21
        • 1970-01-01
        • 1970-01-01
        • 2011-10-31
        • 1970-01-01
        • 1970-01-01
        • 2021-06-29
        • 1970-01-01
        相关资源
        最近更新 更多