Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

removing special characters

by Anonymous Monk
on Feb 13, 2003 at 17:05 UTC ( [id://235027]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks ,
What is the best way to remove any special characters from a $scalar
Thanks Mike

Replies are listed 'Best First'.
Re: removing special characters
by extremely (Priest) on Feb 13, 2003 at 17:50 UTC
    Don't think of it as removing the special characters. Think of it as keeping the safe/useful characters. It is a subtle thing but you'll be well served in the future if you keep that attitude. You can spend all day adding more and more special characters or you can just removed everything that you aren't sure is valid. You'll always miss things but it is easier to allow one extra thing you missed than clean up your database or hacked server when you allowed something you shouldn't have.

    As an example, to keep only upper case letters and numbers, you might do: $scalar =~ s/[^A-Z0-9]+//g; which removes from the string every character that isn't in the A-Z or 0-9 range.

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

      Taint check. It is the only way.™

      my $param = $q->param("text") || ''; #for example... if($param =~ /^([\w\s]+)/){ # modify as needed $param = $1; } else{ return 0; # or die, or croak, or warn, or something! }

      John J Reiser
      newrisedesigns.com

Re: removing special characters
by John M. Dlugosz (Monsignor) on Feb 13, 2003 at 18:13 UTC
    Try the tr///d command instead of s/// if you simply want to remove all occurances of some characters and don't need patterns.

    —John

Re: removing special characters
by Tomte (Priest) on Feb 13, 2003 at 17:09 UTC

    perldoc perlre is your friend
    search for s///

    regards,
    tomte


Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://235027]
Approved by John M. Dlugosz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-29 06:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found