php 为什么strip_tags 在PHP 中不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3780888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:04:49  来源:igfitidea点击:

Why doesn't strip_tags work in PHP?

phpstrip-tags

提问by rushinge

I've got the following code:

我有以下代码:

<?php echo strip_tags($firstArticle->introtext); ?>

Where $firstArticle is a stdClass object:

其中 $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

You can see that $firstArticle->introtext refers to the string:

可以看到 $firstArticle->introtext 指的是字符串:

"<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"

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

The <p>tag is a problem for me in this application, however strip_tags absolutely refuses to remove it and I can't figure out why. I actually gave up on strip_tags and attempted to do a preg_replace instead with the regex /<(.|\n)*?>/ :

<p>标签在此应用中对我的问题,但是用strip_tags绝对拒绝删除它,我想不通为什么。我实际上放弃了 strip_tags 并尝试使用正则表达式 /<(.|\n)*?>/ 执行 preg_replace :

preg_replace('/<(.|\n)*?>/', '', $firstArticle->introtext);

But that didn't work either! How can I strip all HTML tags (matched or not) from this string when I output it?

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

回答by The Surrican

try:

尝试:

<?php echo strip_tags(html_entity_decode($firstArticle->introtext)); ?>

回答by Andreas Linden

very curious that strip-tags does not work....

很好奇条形标签不起作用....

maybe your "<p>" is htmlentity-encoded? like "&lt;p&gt;" (have a look at the page's sourcecode)

也许你的“<p>”是htmlentity编码的?像“<p>” (查看页面的源代码)

otehrwise this will replace all tags, also htmlentity-encoded ones, but it's nearly obvious that this p-tag is simply htmlentity-encoded so try that first...

另外,这将替换所有标签,还有 htmlentity 编码的标签,但很明显这个 p-tag 只是 htmlentity 编码,所以先尝试一下......

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

回答by Aminah Nuraini

In my case, I should use htmlspecialchars_decode($str);. html_entity_decode($firstArticle->introtext)doesn't seem to work for me.

就我而言,我应该使用htmlspecialchars_decode($str);. html_entity_decode($firstArticle->introtext)似乎对我不起作用。

Sometimes I have to use htmlentitiesfirst.

有时我必须先使用htmlentities

        $txt = htmlentities($txt, null, 'utf-8');   
        $txt = htmlspecialchars_decode($txt);