简体   繁体   English

cgi / perl / html-打印成html时要转义哪些字符?

[英]cgi/perl/html - what characters to escape when printing into html?

I got an input file that I need to print directly into an html page. 我有一个输入文件,需要直接将其打印到html页面中。

I did $inputfile =~ s/\\n/<br>/g; 我做了$inputfile =~ s/\\n/<br>/g; Are there any other special characters I should be aware of maybe other than < and > when printing this $inputfile to html? 当将此$ inputfile打印到html时,除了<和>之外,我是否应该注意其他任何特殊字符?

You absolutely should use HTML::Escape instead of doing some ill-conceived hackjob which will cause everyone who deals with your code (you included) to curse your name in the future. 您绝对应该使用HTML::Escape而不是进行一些构思不当的hackjob,这将导致处理您的代码(包括您在内)的每个人将来都诅咒您的名字。

It's simple - install HTML::Escape via CPAN, then use it thus: 很简单-通过CPAN安装HTML::Escape ,然后按以下方式使用它:

use HTML::Escape qw(escape_html);
my $escaped_string = escape_html($string);

Note that if you want to preserve whitespace formatting you should use a module to do that, as well, such as HTML::FromText - the above code will not automagically convert line breaks to 请注意,如果您想保留空格格式,也应该使用模块来执行,例如HTML::FromText上面的代码不会自动将换行符转换为
tags because that's different completely from escaping unsafe characters to HTML entities. 标签,因为这与将不安全字符转义为HTML实体完全不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM