Solved

Populating a textarea with a script

Posted on 2004-03-29
15
389 Views
Last Modified: 2009-07-29
I have a script that populates a textarea with what ever file you will like to read.  YOu can view html, txt, pl, cgi etc.

The only problem is when I read a HTML or scripting lang into a text area, if that file has a text area in it will not load right, the textarea in the file closes the textarea in the script.

here is an example.

Test.html.

<html>
</body>
<textarea>Hello world</textarea>
blah blah blah blha
<input type="text" name="blah" id="blah">
blahb blahb
</body>
</html>

Now when I load test.html into the script which will display test.html in the text area.  The end tag for the textarea in Test.html will close the text area in the scrip that is reading the test.html.

I know this sounds confusing.  But if anyone could help that would be great!
0
Comment
Question by:Andrew99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 

Author Comment

by:Andrew99
ID: 10702539
Here are the results of test.html being displayed in the textarea.

----------------------------------
<html>                             |
</body>                           |
<textarea>Hello world       |
------------------------------  |
                                       
blah blah blah blha
<input type="text" name="blah" id="blah">
blahb blahb
</body>
</html>


You see the test.html </textarea> is closing off the scripts text area.
0
 
LVL 18

Expert Comment

by:kandura
ID: 10703173
You should html-escape the contents of the file. At the very minimum, replace < signs into &lt;

$content =~ s/</\&gt;/g;
0
 

Author Comment

by:Andrew99
ID: 10703334
This is how I open my file for reading.

open(INF, $file) or die("Unabled to open Dat file");
@raw_data=<INF>;
close(INF);

How do I implement the $content =~ s/</\&gt;/g; ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Accepted Solution

by:
kandura earned 25 total points
ID: 10703526
there are several options. I'd say, loop over the array @raw_data and modify each element:

@escaped_data = map { s/</\&lt;/g; $_ } @raw_data;

and then print out @escaped_data where you previously printed @raw_data.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10704134
It's a little unclear what you want to see in the textbox when you display HTML content. Are you looking to see the HTML rendered by the browser or as HTML source?

For a reversible transformation, something like the following might do:

my $raw_data;
{
open INF, $file or die "Unable to open $file -- $!";
local $/; # enable 'slurp' mode
$raw_data = <INF>;
close INF;
}
$raw_data =~ s/&/\&amp;/g; # first escape exising ampersands
$raw_data =~ s/</\&gt;/g; # escape possible HTML tag openings

===========================================

Now $raw_data contains a string that should display HTML source text with no danger of misinterpretation. The transform can also be reversed to get back the original text, if you needed to do that.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10709183
Following on from jmcg's suggestion.  To correct escape all HTML characters, you can do:


use CGI ':standard';

my $raw_data;
{
open INF, $file or die "Unable to open $file -- $!";
local $/; # enable 'slurp' mode
$raw_data = <INF>;
close INF;
}

$raw_data = escapeHTML($raw_data);


0
 

Author Comment

by:Andrew99
ID: 10730876
Kandura  Thanks that worked.  I forgot to ask what would I use to reverse back to the orginal?
0
 
LVL 18

Expert Comment

by:kandura
ID: 10731047
assuming you receive the new value through param('name_of_textarea'), you would do:
my $new_data = param('name_of_textarea');
$new_data =~ s/\&lt;/</g;       # unescape <
$new_data =~ s/\&amp;/\&/g;     # unescape &
0
 

Author Comment

by:Andrew99
ID: 10731425
I am not sure why the unescaping is not working.  
0
 

Author Comment

by:Andrew99
ID: 10731427
so I need to unescape it when I right it to the database.  
0
 

Author Comment

by:Andrew99
ID: 10731454
What happens is I load the file up, and save it, load it up again and it doesn't escape the html.
this is how I write the file.
sub save_file{
   $Source =~ s/\&lt;/</g;       # unescape <
   $Source =~ s/\&amp;/\&/g;     # unescape &
   open uDAT,">" , $filePath or die "Cannot Open File";
  print uDAT $Source;
  close uDAT;
}

This is how I read it.
open(INF, $filePath) or die("Unabled to open Dat file");
@raw_data=<INF>;
close(INF);

@escaped_data = map { s/</\</g; $_ } @raw_data;

Now it works once, but then when I do the exact same page again it does not escape the html???
0
 
LVL 18

Expert Comment

by:kandura
ID: 10731501
you would escape the html with these regular expressions, not s/</\</g  :

@escaped_data = map {
                s/&/\&amp;/g;   # first escape existing ampersands
                s/</\&lt;/g;       # escape possible HTML tag openings
                $_;
            }
            @raw_data;

Note that both me and jmcg had a typo in the original answers: we wanted to change < into &gt;, which translates back to > ;^)
0
 

Author Comment

by:Andrew99
ID: 10731516
The unescape code is correct tho right?
0
 
LVL 18

Expert Comment

by:kandura
ID: 10731549
yes, it is.

Note that this only (un)escapes only those two entities. It may be enough for your immediate needs, but if you find out there are other things you need to (un)escape, then I suggest you take a look at the HTML::Entities package. It handles the more general case.

Here's a snippet of code from the documentation:

use HTML::Entities;
$encoded = encode_entities($a);   #### encodes unsafe characters to html entities (eg. é -> &eacute;)
$decoded = decode_entities($a);   #### decodes html entities back to regular characters (eg. &lt; -> < )
0
 

Author Comment

by:Andrew99
ID: 10731571
Thank you very much for you help.  :)
I will read up on html Entities.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question