aQ Posted May 18, 2007 Share Posted May 18, 2007 Hello! Are there any functions that can be used to delete all other characters than number in a form or a variable? Quote Link to comment Share on other sites More sharing options...
desithugg Posted May 18, 2007 Share Posted May 18, 2007 Well I don't know about deleting but you can use is_int($num); to check if the number is an integer Quote Link to comment Share on other sites More sharing options...
aQ Posted May 18, 2007 Author Share Posted May 18, 2007 What about minus or plus? Will it show as an integer anyway? Quote Link to comment Share on other sites More sharing options...
taith Posted May 18, 2007 Share Posted May 18, 2007 or... is_numeric() Quote Link to comment Share on other sites More sharing options...
aQ Posted May 18, 2007 Author Share Posted May 18, 2007 If I use "is_numeric()", it accepts + and -. If I use "is_int($num)" it doesn't any numbers + or -. Any other ways? ??? Quote Link to comment Share on other sites More sharing options...
taith Posted May 18, 2007 Share Posted May 18, 2007 declaring the string an int... $string=213hlsdf34; $int=(INT)$string; Quote Link to comment Share on other sites More sharing options...
Orio Posted May 18, 2007 Share Posted May 18, 2007 If you want to delete all of the non-numeric chars, use: $only_nums = preg_replace("/[^0-9]/", "", $string); If you only want to validate the string is numeric I'd use: <?php if(!is_numeric($num) || !preg_match("/^[0-9]+$/", $num)) { //Invalid }else{ //Valid } ?> Orio. Quote Link to comment Share on other sites More sharing options...
aQ Posted May 18, 2007 Author Share Posted May 18, 2007 I'm feeling stupid now... To ask in another way: Are there any way that I can search for a letter in a variable? Quote Link to comment Share on other sites More sharing options...
Orio Posted May 18, 2007 Share Posted May 18, 2007 I am not sure what you mean, but take a look at strpos(). Orio. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.