如何使用Perl去除字符串中的HTML? [英] How can I strip HTML in a string using Perl?
本文介绍了如何使用Perl去除字符串中的HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有比这更容易使用Perl去除字符串中的HTML的?
$ Error_Msg =〜s |< b个|| IG;
$ Error_Msg =〜s |< / b> || ig;
$ Error_Msg =〜s |< h1> || ig;
$ Error_Msg =〜s |< / h1> || ig;
$ Error_Msg =〜s |< br> || ig;
我会同时提供一个瘦身的正则表达式,例如像这样的东西:
$ Error_Msg =〜s |< /?[b | h1 | br]> || ig ;
是否有一个现有的Perl函数可以从字符串中去除任何/所有的HTML,尽管我只需要假设代码是有效的HTML(没有流浪< or>运算符)
p> $ htmlCode =〜s |<。+?> || g;
如果您只需要移除粗体,h1和br的
$ htmlCode =〜s#< /?(?: b | h1 | br)\ b。*?> ## g
您可能需要考虑 HTML :: Strip 模块
Is there anyway easier than this to strip HTML from a string using Perl?
$Error_Msg =~ s|<b>||ig;
$Error_Msg =~ s|</b>||ig;
$Error_Msg =~ s|<h1>||ig;
$Error_Msg =~ s|</h1>||ig;
$Error_Msg =~ s|<br>||ig;
I would appreicate both a slimmed down regular expression, e.g. something like this:
$Error_Msg =~ s|</?[b|h1|br]>||ig;
Is there an existing Perl function that strips any/all HTML from a string, even though I only need bolds, h1 headers and br stripped?
解决方案
Assuming the code is valid HTML (no stray < or > operators)
$htmlCode =~ s|<.+?>||g;
If you need to remove only bolds, h1's and br's
$htmlCode =~ s#</?(?:b|h1|br)\b.*?>##g
And you might want to consider the HTML::Strip module
这篇关于如何使用Perl去除字符串中的HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文