php 如何从字符串中删除 <br /> 标签等?

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

How to remove <br /> tags and more from a string?

php

提问by Pekka

I need to strip all <br />and all 'quotes' (") and all 'ands' (&) and replace them with a space only ...

我需要去除所有<br />和所有“引号”(")和所有“和”(&)并仅用空格替换它们......

How can I do this? (in PHP)

我怎样才能做到这一点?(在 PHP 中)

I have tried this for the <br />:

我已经尝试过这个<br />

   $description = preg_replace('<br />', '', $description);

But it returned <>in place of every <br />...

但它<>代替了每个<br />......

Thanks

谢谢

采纳答案by Konamiman

To manipulate HTML it is generally a good idea to use a DOM aware tool instead of plain text manipulation tools (think for example what will happen if you enounter variants like <br/>, <br />with more than one space, or even <br>or <BR/>, which altough illegal are sometimes used). See for example here: http://sourceforge.net/projects/simplehtmldom/

为了操作 HTML,使用 DOM 感知工具而不是纯文本操作工具通常是一个好主意(例如,如果您遇到像<br/><br />多个空格,甚至<br>或等变体会发生什么<BR/>,有时使用这些变体是 非法的)。例如,请参见此处:http: //sourceforge.net/projects/simplehtmldom/

回答by Thevsuman

<?php

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');

?>

http://php.net/manual/en/function.strip-tags.php

http://php.net/manual/en/function.strip-tags.php

回答by Gordon

If str_replace()isnt working for you, then something else must be wrong, because

如果不str_replace()适合你,那么一定是别的什么地方出了问题,因为

$string = 'A string with <br/> & "double quotes".';
$string = str_replace(array('<br/>', '&', '"'), ' ', $string);
echo $string;

outputs

产出

A string with      double quotes .

Please provide an example of your input string and what you expect it to look like after filtering.

请提供输入字符串的示例以及过滤后的外观。

回答by Vincent Ramdhanie

You can use str_replacelike this:

您可以像这样使用str_replace

  str_replace("<br/>", " ", $orig );

preg_replace etc uses regular expressions and that may not be what you want.

preg_replace 等使用正则表达式,这可能不是您想要的。

回答by Pekka

To remove all permutations of br:

要删除 br 的所有排列:

<br> <br /> <br/> <br   >

check out the user contributed strip_only()function in

查看用户贡献的strip_only()功能

http://www.php.net/strip_tags

http://www.php.net/strip_tags

The "Use the DOM instead of replacing" caveat is always correct, but if the task is really limited to these three characters, this should be o.k.

“使用 DOM 而不是替换”警告总是正确的,但如果任务真的仅限于这三个字符,这应该没问题

回答by Art3mk4

Try this:

尝试这个:

$description = preg_replace('/<br \/>/iU', '', $description);

回答by pete

This worked for me, to remove <br/>:

这对我有用,可以删除<br/>

(&gt;is recognised whereas > isn't)

(&gt;被识别而 > 不是)

$temp2 = str_replace('&lt;','', $temp);

// echo ($temp2);

$temp2 = str_replace('/&gt;','', $temp2);

// echo ($temp2);

$temp2 = str_replace('br','', $temp2);

echo ($temp2);