Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've got a script that takes a string and prints it into a text field on a form, and often that string will have html tags in it. If the string is like this:

<span class = "blah">

It will offset the text field. Any suggestions on how I can handle this?

Replies are listed 'Best First'.
Re: Printing quotes out to an html form?
by Fastolfe (Vicar) on Jan 02, 2001 at 20:23 UTC
    There are a few ways you can do this:
    1. Scrap your code and build it with CGI instead. Attribute arguments are already automatically quoted for you.
    2. Use your existing code in conjunction with the CGI module. At a minimum, you can use &CGI::escapeHTML, though I'm not sure that this is a function meant for public consumption.
    3. Just write some code to do your own escaping. s/"/\\"/g;
      Im using CGI, but the string is taken from a text file and printing to a form, not the other way around.
        Well then, follow Fastolfe's second suggestion.
        use CGI; # you already have this line $q = new CGI; # and one like this to I suppose my $var = "some text from a file"; my $varesc = $q->escapeHTML($var);

        --
        $you = new YOU;
        honk() if $you->love(perl)

        well, if you are using CGI.pm then why not use the textfield method? it escapes html for you (it uses the escapeHTML() method internally):
        use CGI; my $q = new CGI; my $value = '<img src="/someimg.gif">'; print $q->start_html(), $q->start_form(), $q->textfield( -name=>'test', -value=>$value), $q->end_form, $q->end_html;
        it works great!
Re: Printing quotes out to an html form?
by epoptai (Curate) on Jan 02, 2001 at 23:34 UTC
    I don't know why but escapeHTML doesn't always work for me. But I can always get code to render in a textarea or hidden form field by using HTML::Entities.
    use HTML::Entities; &encode_entities($string)
    And if you need to decode:
    &decode_entities($string)