0

Is there a function in PERL (don't care if it's a built-in function or a module-provided function) that will accept a string containing a section of HTML code as it's argument --- and as it's return value provide a string of newly-generated HTML code that, when sent to the browser, will show the user the same HTML code that originally was provided to this function as an argument?

For example, if the argument provided to this function is

<p>This is <em>really</em>
what I call "awesome" & then some.</p>

I would like the return-value to be something alone the lines of

&lt;p&gt;This is &lt;em&gt;really&lt;/em&gt;<br/>what
I call &quot;awesome&quot; &amp; then some.&lt;/p&gt;

So - does there exist a function in Perl that will do this for me?

1

1 Answer 1

1

Your best choice is probably the HTML::Escape module

Use it like this

use strict;
use warnings 'all';

use HTML::Escape 'escape_html';

my $html = <<END_HTML;
<p>This is <em>really</em>
what I call "awesome" & then some.</p>
END_HTML

print escape_html($html);

output

&lt;p&gt;This is &lt;em&gt;really&lt;/em&gt;
what I call &quot;awesome&quot; &amp; then some.&lt;/p&gt;
2
  • Okay --- it doesn't HTML-substitute the newline character -- but at least it does everything else I want.
    – Sophia_ES
    Mar 27, 2016 at 20:59
  • Wrap it in a pre element (or equivalent CSS) to preserve newlines and multiple whitespace
    – ysth
    Mar 28, 2016 at 1:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.