为什么strip_tags不能在PHP中运行?

rus*_*nge 16 php strip-tags

我有以下代码:

<?php echo strip_tags($firstArticle->introtext); ?>
Run Code Online (Sandbox Code Playgroud)

其中$ firstArticle是一个stdClass对象:

object(stdClass)[422]
  public 'link' => string '/maps101/index.php?option=com_content&view=article&id=57:greenlands-newest-iceberg&catid=11:geography-in-the-news' (length=125)
  public 'text' => string 'GREENLAND'S NEWEST ICEBERG' (length=26)
  public 'introtext' => string '<p>A giant chunk of ice calved off the Petermann Glacier on

    the northwest side of Greenland this summer. At nearly 100 square miles (260

    sq. km) in size, four times the size of Manhattan, th' (length=206)
  public 'date' => 
    object(JDate)[423]
      public '_date' => int 1284130800
      public '_offset' => int 0
      public '_errors' => 
        array
          empty
Run Code Online (Sandbox Code Playgroud)

你可以看到$ firstArticle-> introtext引用了字符串:

" <p>今年夏天,格陵兰岛西北侧的彼得曼冰川出现了一大块冰.大约100平方英里(260平方公里),是曼哈顿面积的四倍,"

<p>标签在此应用中对我的问题,但是用strip_tags绝对拒绝删除它,我想不通为什么.我实际上放弃了strip_tags并试图用regex /<(.|\n)*?> /来做一个preg_replace:

preg_replace('/<(.|\n)*?>/', '', $firstArticle->introtext);
Run Code Online (Sandbox Code Playgroud)

但这也不起作用!当我输出时,如何从该字符串中删除所有HTML标记(匹配与否)?

The*_*can 61

尝试:

<?php echo strip_tags(html_entity_decode($firstArticle->introtext)); ?>
Run Code Online (Sandbox Code Playgroud)

  • 如果字符串编码了 html 实体,它实际上不包含任何标签,因此该行为是预期的。实际上,如果您在应用程序中使用字符串以防止 xss 注入和类似问题,您应该确保重新编码字符串 (3认同)

And*_*den 6

非常奇怪的是条形标签不起作用....

也许你的"<p>"是htmlentity编码的吗?喜欢"< p>" (看看页面的源代码)

otehrwise这将取代所有标签,也取代htmlentity编码的标签,但几乎显而易见的是,这个p标签只是简单的编码,所以首先尝试...

preg_replace('/(?:<|&lt;).*?(?:>|&gt;)/', '', $firstArticle->introtext);
Run Code Online (Sandbox Code Playgroud)