View Full Version : Cleaning form submited strings

07-28-2002, 01:41 PM
Hi Friends,
I need a method for cleaning strings from all NON alphabetic and numeric characters that are submitted by a form

I have the following strings:
$data1 - $data2 and $data3
before writing these 3 strings to file, I want to eleminate all characters that are not a-z, A-Z and 0 to 9 and replace them with nothing, not even a blank.
Meaning if $data1 = "my tel # 025/123.895.548"
should become $data1 = "my tel 025123895548"

Then I need the same as above but also eleminating blank spaces
such as:
$data5 = "map &125 #abc <small"
$data5 = "map25abcsmall"

And to make things even worse I need a very difficult one
That eleminates everything that is not a number or a decimal point.
$data6 = "Temperature for city $manoi <b>40.08</b> celcius"
$data6 = "40.08"

Best Regards,

07-28-2002, 07:51 PM
$data1 =~ s/[^ 0-9A-Za-z]//g;
$data2 =~ s/[^ 0-9A-Za-z]//g;
$data3 =~ s/[^ 0-9A-Za-z]//g;
$data5 =~ s/[^0-9A-Za-z]//g;
$data6 =~ s/[^0-9]//g;The last one will only do what you asked, that is, it will also change "2.5+3.7=6.2" into "".

07-28-2002, 08:52 PM
Thanks alot Centaur.
I will try it out tomorrow and let you know if it works (for sure it will).
I am of to sleep now, it is here 2 AM in the morning.

Thanks Centaur and Regards,

07-29-2002, 08:25 AM
Hi friends,
Thanks, it works !!

But now I need just the opposite for a fill-in form.

$minchar = '10';
$maxchar = '20';
if(length $FORM{'data8'} < $minchar || length $FORM{'data8'} > $maxchar) { &errormessage }

1/How do I add to the above that the string may ONLY contain letters a-z and A-Z and/or numbers 0-9, nothing else.

2/completely same as above, but may also not contain any spaces.

Thanks and Regards,

07-29-2002, 03:51 PM
Get the manual and read the perlre part. Understanding regular expressions is essential for text analysis.
&hellip; || $FORM{'data8'} !~ /^[0-9A-Za-z]*$/ || &hellip;This subexpression evaluates to true if $FORM{'data8'} does not match (!~) the regular expression /^[0-9A-Za-z]*$/, which means: ^ &mdash; start of text,
[0-9A-Za-z] &mdash; digit or letter,
* &mdash; repeated 0 or more times,
$ &mdash; end of text.