Forum Moderators: coopster & phranque

Message Too Old, No Replies

checking if a string contains non-numeric characters

is there an easy way to do it?

         

rjstephens

7:52 am on Mar 6, 2004 (gmt 0)

10+ Year Member



is there a way to check a string to see if it contains anything other than numeric characters? If so, how is it done.
Any help greatly appreciated.

tombola

8:20 am on Mar 6, 2004 (gmt 0)

10+ Year Member



\D matches anything that is NOT a number.
\d matches any digit.

$test = "123test";
if ($test!~ /\D/) {
print "String contains only digits";
} else {
print "String contains other characters";
}

rjstephens

10:20 am on Mar 7, 2004 (gmt 0)

10+ Year Member



strange. That code you gave seems to work, but any print statements I put inside the {}s don't get executed for some reason. I ahve print statements elswehere in the script and they work fine. As long as the other things get done i guess it doesn't really matter.

rjstephens

10:36 am on Mar 7, 2004 (gmt 0)

10+ Year Member



thanks for the help. sorry about the directly above post. On revision I found that nothing was working at all.

After discovering the problem, however, I feel like banging my head onto the desk until i'm unconsions. If anyone is interested, here's what was going on.

Inside the first if statement (the one to check for digits in the query) there was a SQL statement, which was inside an $SQL=<<"EOT"; statement. Now, the thing was, the EOT was indented. So it treated the EOT as part of the text, not an isntruction to end it. The only reason I didn't get errors pointing me to it was because there was another EOT connected to another SQL query which was inside another if statement further down in the file. So about 3 quarters of my code was being passed to mysql rather than being evaluated. I feel so stupid.

The only awy I found out was because I DID get an error when I started to randomly comment out large sections of the code.

Man I feel so stupid. How could I not have seen this earlier, this problem has been there since I started work on this script.

Again, thanks for that solution.