dongnaosuan5407
2016-08-09 19:25
浏览 27
已采纳

Perl相当于htmlentities()

I am new to Perl.

I am trying to duplicate some/most of the functionality of PHP's htmlentities()

So far I have this:

$str = '" \' < >';

$str = join('&lt;', split('<', $str));
$str = join('&gt;', split('>', $str));
$str = join('&quot;', split('"', $str));
$str = join('&#039;', split("'", $str));

print $str;

It is working for my needs but the micro-optimizer in me just feels like there must be a better way.

I am terrible with regex and so many Perl tutorials deal with custom or non-standard libraries or it just seems this way due to my inexperience.

perl -v produces:

v5.10.1

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douchen7324 2016-08-09 19:27
    已采纳

    Use HTML::Entities's encode_entities. (It obviously won't be 100% equivalent since the beavhour of htmlentities varies based on the arguments passed to it.)

    点赞 打赏 评论
  • dtml3340 2016-08-09 19:28

    Hey what you asked for can be achieved by using decode_entities()

    Example code:-

    use HTML::Entities; my $html = "Hey &amp; &lt"; print decode_entities($html), "
    ";
    

    Use this as a reference :- Reference

    点赞 打赏 评论

相关推荐 更多相似问题