CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Stripping wierd characters

 



BikerHQ
Deleted

Dec 6, 2000, 8:02 AM

Post #1 of 7 (1309 views)
Stripping wierd characters Can't Post

Hi there,

I'm completely new at the perl business. I've just written my first little script for my webpage - a guestbook no less!

I've got everything working perfectly apart from the fact that I can't seem to get my head round stripping control characters from strings. I've got these four fields....

gbname, gbemail, gbfindus and gbtext

If you go to my site http://www.bikerhq.co.uk and go right to the bottom of the page you'll see a small "test" link. That's my guestbook. You'll see that the entries still contain funny characters.

I need to know how to strip them if anyone can help.

Thanks for your time,
Si

p.s. I'll give bonus points if someone can tell me how to open a file and add lines to the top instead of appending to the bottom!

Told you I was a beginner!


[This message has been edited by BikerHQ (edited 12-06-2000).]


BikerHQ
Deleted

Dec 5, 2000, 10:16 PM

Post #2 of 7 (1309 views)
Re: Stripping wierd characters [In reply to] Can't Post

I'm afraid I'm a little lost. I'll try to explain myself a bit clearer...

I just added an entry to my guestbook which basically contained all the characters on my keyboard which aren't letters or numbers. It displays like this....

"%0D%0A%0D%0A%21 %22 %A3 %24 %25 %5E %26 * %28 %29 %7B %7D %5B %5D %3A %3B @ %27 %7E %23 %3C %2C %3E . %3F %2F %7C %5C"

If I wanted to take that string and change it to display the actual characters those codes represent, how would I do it? (I think that's a better explanation!)

I can send my guestbook code if it helps you any. It's probably pretty crappy, cos it's my first ever perl script - and it definately don't be very server friendly or optimised like it should be!

Thank you both again for your much needed help!


sleuth
Enthusiast

Dec 6, 2000, 8:54 AM

Post #3 of 7 (1309 views)
Re: Stripping wierd characters [In reply to] Can't Post

 
Use s///; for that.

$form{'gbname'} =~ s!%!!g;

That will remove all % in the name field, also, to avoid having to write a line for each character you want to remove, do this:

$form{'gbname'} =~ s![%|\$|\@|\!|~|\`|(|)]!!g;

By using brackets [] to enclose the characters separated by | meaning or.

Don't forget to escape characters that need it. Like @.

To write to the tope of the file you have to first read what is currently in the file, then overwrite the file with the information you want at the top, then append back the origional data that was in the file.

Example:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open(file, "<data.db") &#0124; &#0124; print "$!";
@origional_data=<file>;
close(file);
open(write, ">data.db") &#0124; &#0124; print "$!";
print write "$form{'gbname'}|$form{'gbemail'}|$form{'gbfindus'}|$form{'gbtext'}|\n";
print write @origional_data;
close(write);
</pre><HR></BLOCKQUOTE>

See,

Sleuth


BikerHQ
Deleted

Dec 6, 2000, 9:08 AM

Post #4 of 7 (1309 views)
Re: Stripping wierd characters [In reply to] Can't Post

You're a genius Sleuth. Thanks for the info. You know, I read a huge book about Perl and they didn't seem to explain the character removal half as well as you just did in a few lines.

nice work bud.

:)

p.s. I suspect you will be hearing from me again soon!


BikerHQ
Deleted

Dec 6, 2000, 9:28 AM

Post #5 of 7 (1309 views)
Re: Stripping wierd characters [In reply to] Can't Post

Sleuth.... just one more thing. The examples you gave above to strip characters. Does that remove them or change them to what they formerly were?

Clear as mud aren't I. What I mean to say is that people who enter things into the guestbook will use characters in their text such as ! ; : etc. these get converted to codes on the URL line. When I come to read the flatfile I've got storing the data, I want to convert them back so people see what they originally typed.

I haven't tried it yet to see what it does do, but I shall.


sleuth
Enthusiast

Dec 6, 2000, 9:41 AM

Post #6 of 7 (1309 views)
Re: Stripping wierd characters [In reply to] Can't Post

 
Thanks BikerHQ,

things like

$form{'gbname'} =~ s![%|\$|\@|\!|~|\`|(|)]!!g;

Will not help you to convert characters to assci characters only to convert them back later, you would have to do something like this,

$form{'gbname'} =~ s!%!-P-!g;
$form{'gbname'} =~ s!\$!-MO-!g;

Then write that info to the data base,

When you read it back convert the fields back to origional information,

$form{'gbname'} =~ s!-P-!%!g;
$form{'gbname'} =~ s!-MO-!$!g;

See, left to right, i didn't escape the $ the second time around because unless you tack an e next to the g the right side is treated in a literal way. :)

Hope that helps,

Sleuth


japhy
Enthusiast

Dec 6, 2000, 9:42 AM

Post #7 of 7 (1309 views)
Re: Stripping wierd characters [In reply to] Can't Post

Agh, sleuth! I have a few comments about that code.

First, you do NOT need to use a | inside a character class, because character classes are just that -- options of CHARACTERS to match. In fact, by placing a | in a character class, you make it a valid character to be matched:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# attempt to check if $word has only the
# characters 'a', 'b', and 'c'
$word = "a|c";
print "$word is ok" if $word =~ /^[a|b|c]+$/;
</pre><HR></BLOCKQUOTE>

That will succeed.

And PLEASE PLEASE PLEASE use UPPERCASE names for your filehandles. write happens to be the name of a Perl function. In fact, your code does not run, and produces an error:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open write, ">foo";
print write "this";
close write;
</pre><HR></BLOCKQUOTE>

Gives me Undefined format "main::STDOUT" called at ... line 1.

And finally, the question of inserting lines into a file is a FAQ (so look for it in section 5 of the FAQ, under the title "How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?").

Here is a simple way to insert a line at the beginning of a file, using Perl's built-in in-place file handling:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


{
local $^I = ".bak"; # to keep a backup copy, just in case
local @ARGV = "file_to_modify.txt";
while (<> ) {
print "THIS IS THE NEW FIRST LINE.\n" if $. == 1;
print;
}
}
</pre><HR></BLOCKQUOTE>

And it appears that what Biker wants is URL decoding. This can be done automatically with any proper CGI query parser, like the CGI.pm module that comes with Perl:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use CGI 'param';
$first_name = param('name');
# $first_name is now URL-decoded
</pre><HR></BLOCKQUOTE>

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives