MonkeyZeus MonkeyZeus - 3 months ago 14
Perl Question

Perl equivalent to htmlentities()

I am new to Perl.

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



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





It's possible that this question belongs on CodeReview but I'm not sure yet.

Answer

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.)

Comments