php PHP从字符串中删除html标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15309077/
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 09:01:45  来源:igfitidea点击:

PHP removing html tags from string

phphtml

提问by Wizard

I have string:

我有字符串:

<p justify;"="">Vers-lo cent-rai Lie-tu-vos ne-kil-no-ja-mo-jo turto pl?t-ros aso-cia-ci-jos kon-kur-se  ...</p>

and want want remove tag

并且想要移除标签

<p justify;"=""></p>

my code:

我的代码:

$content = strip_tags($text, '<p>');

but i get empty string: string(0) "", what I do wrong ?

但我得到空字符串:string(0) "",我做错了什么?

回答by Toretto

Try to put it like that

试着这样说

$content = strip_tags($text);

Or you can do it with regular expression like that:

或者你可以用这样的正则表达式来做到这一点:

$content = preg_replace('/<[^>]*>/', '', $text);

By this $content = strip_tags($text, '<p>');you are allowing the <p>tag in the string.

这样$content = strip_tags($text, '<p>');你就允许<p>在字符串中使用标签。

For more info see the link http://php.net/manual/en/function.strip-tags.php

有关更多信息,请参阅链接http://php.net/manual/en/function.strip-tags.php

回答by Magnus Lindgren

Since the HTML is poorly formated you probably need to either write your own regexp to remove tags or clean up the HTML before trying to remove tags.

由于 HTML 格式不佳,您可能需要编写自己的正则表达式来删除标签或在尝试删除标签之前清理 HTML。

You could try this to remove everything that "looks like" a tag:

您可以尝试删除所有“看起来像”标签的内容:

$str = preg_replace("/<.*?>/", " ", $str);

回答by Wilf

This will remove every thing - tags, ascii, line breaks but pure text:

这将删除所有内容 - 标签、ascii、换行符但纯文本:

strip_tags(preg_replace('/<[^>]*>/','',str_replace(array("&nbsp;","\n","\r"),"",html_entity_decode($YOUR_STRING,ENT_QUOTES,'UTF-8'))));

回答by Mihai Iorga

Since your HTML is not properly formatted you could choose a preg_replace()approach:

由于您的 HTML 格式不正确,您可以选择一种preg_replace()方法:

$text = '<p justify;"="">Vers-lo cent-rai Lie-tu-vos ne-kil-no-ja-mo-jo turto pl?t-ros aso-cia-ci-jos kon-kur-se ... </p>';
$content = preg_replace('/<[^>]*>/', '', $text); 
var_dump($content);
// string(108) "Vers-lo cent-rai Lie-tu-vos ne-kil-no-ja-mo-jo turto pl?t-ros aso-cia-ci-jos kon-kur-se ... "

Codepad Example

键盘示例

On strip_tags() docsit says: Because strip_tags() does not actually validate the HTML, partial or broken tags can result in the removal of more text/data than expected.

strip_tags() 文档上它说:因为 strip_tags() 实际上并不验证 HTML,部分或损坏的标签可能会导致删除比预期更多的文本/数据。

Also second parameter is for $allowable_tags.

第二个参数也是用于$allowable_tags.

回答by APetrovsky

This will replace all html tags, https://regex101.com/r/jM9oS4/4

这将替换所有 html 标签, https://regex101.com/r/jM9oS4/4

preg_replace('/<(|\/)(?!\?).*?(|\/)>/',$replacement,$string);

回答by Fellipe Sanches

From PHP 7.4.0 the strip_tags() alternatively accepts an array with allowable tags,

从 PHP 7.4.0 开始, strip_tags() 或者接受带有允许标签的数组,

then this:

那么这个:

<?php

$html = '<div id="my-div"><p>text<strong><a href="#link"></a></strong></p></div>';

echo strip_tags($html, ['p', 'a']); //accept p and a tags

Return this:

返回这个:

<p>text<a href="#link"></a></p>

Note that only the disallowed tags have been removed.

请注意,仅删除了不允许的标签。