thara Posted November 3, 2012 Share Posted November 3, 2012 can anybody tell me how can check values in an array have only 4 digits long numbers. if not, if there are strings or numbers long to more than 4 digits, I want to echo an error massege. This is Correct one : $searchText = '3423, 2453, 3245 , 2425, 6765'; This is Wrong one : $searchText = '34d23, 244353, fsddf , 2d4425, 674365'; can any body tell me how can I do this? thank you... Quote Link to comment https://forums.phpfreaks.com/topic/270239-checking-array-values-are-numbers/ Share on other sites More sharing options...
DarkerAngel Posted November 3, 2012 Share Posted November 3, 2012 (edited) You could always try your results against is_int. Then if you want to know if it's 4 digits after validating that it is an integer value you could always try: if ( $value >= 1000 && $value <= 9999 ) { //Code true } else { //Code false / error message } Edited November 3, 2012 by DarkerAngel Quote Link to comment https://forums.phpfreaks.com/topic/270239-checking-array-values-are-numbers/#findComment-1389888 Share on other sites More sharing options...
JohnTipperton Posted November 3, 2012 Share Posted November 3, 2012 here is a list of function you could use is_bool() - Finds out whether a variable is a boolean is_float() - Finds whether the type of a variable is float is_numeric() - Finds whether a variable is a number or a numeric string is_string() - Find whether the type of a variable is string is_array() - Finds whether a variable is an array is_object() - Finds whether a variable is an object Quote Link to comment https://forums.phpfreaks.com/topic/270239-checking-array-values-are-numbers/#findComment-1389891 Share on other sites More sharing options...
gizmola Posted November 3, 2012 Share Posted November 3, 2012 This is Correct one : $searchText = '3423, 2453, 3245 , 2425, 6765'; This is Wrong one : $searchText = '34d23, 244353, fsddf , 2d4425, 674365'; thank you... Those are not arrays, they are strings. Your question is far from clear. Does a particular number have to be exactly 4 digits or can it be from 1-3 digits long? What is the output you expect? Quote Link to comment https://forums.phpfreaks.com/topic/270239-checking-array-values-are-numbers/#findComment-1389893 Share on other sites More sharing options...
thara Posted November 3, 2012 Author Share Posted November 3, 2012 yes gizmola.. Its not an array. It is a sting that comes from a form. What I need to do is users can enter 4 digits long numbers separating by commas then I need to collect them. And want check these are numbers or its contain strings or long than 4 digits etc.. Thank you.. Quote Link to comment https://forums.phpfreaks.com/topic/270239-checking-array-values-are-numbers/#findComment-1389895 Share on other sites More sharing options...
gizmola Posted November 3, 2012 Share Posted November 3, 2012 Some code like this is what you want: $numbers = explode(',', $searchText); foreach ($numbers as &$number) { // make sure you don't have any spaces in the string $number = trim($number); if (is_int($number) && strlen($number) == 4) { echo "$number is ok"; } else { echo "$number is not a 4 digit number"; } } Quote Link to comment https://forums.phpfreaks.com/topic/270239-checking-array-values-are-numbers/#findComment-1389903 Share on other sites More sharing options...
thara Posted November 3, 2012 Author Share Posted November 3, 2012 I got a solution to this using this function.. $searchText = $_POST['searchText']; function validate($input) { $searchArray = array_map('trim', explode(',', $input)); foreach($searchArray as $item) { if(filter_var($item, FILTER_VALIDATE_INT) === false || strlen($item) != 4) return false; } return true; } if(!validate($searchText)) { $searchingError= 'Enter four digits length number!'; echo '<script type="text/javascript">'; echo "alert('" . $searchingError . "')"; echo "</script>"; } else { echo $searchText; } can I know this a good way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/270239-checking-array-values-are-numbers/#findComment-1389910 Share on other sites More sharing options...
haku Posted November 3, 2012 Share Posted November 3, 2012 What's wrong with the way Gizmola showed you, and the method you showed yourself? Both should work. Quote Link to comment https://forums.phpfreaks.com/topic/270239-checking-array-values-are-numbers/#findComment-1389915 Share on other sites More sharing options...
salathe Posted November 3, 2012 Share Posted November 3, 2012 What's wrong with the way Gizmola showed you Apart from never reaching the "is ok" code... not a lot. Quote Link to comment https://forums.phpfreaks.com/topic/270239-checking-array-values-are-numbers/#findComment-1389928 Share on other sites More sharing options...
jazzman1 Posted November 3, 2012 Share Posted November 3, 2012 Apart from never reaching the "is ok" code... not a lot. Yep, the code could be something like that, $numbers = array_map('intval',explode(',', trim($searchText))); foreach ($numbers as &$number) { if (is_int($number) && strlen($number) == 4) { echo "$number is ok"; } else { echo "$number is not a 4 digit number"; } } Quote Link to comment https://forums.phpfreaks.com/topic/270239-checking-array-values-are-numbers/#findComment-1389937 Share on other sites More sharing options...
salathe Posted November 3, 2012 Share Posted November 3, 2012 Yep, the code could be something like that, Except that that would be more forgiving than the OP has stated is allowed thus far. Quote Link to comment https://forums.phpfreaks.com/topic/270239-checking-array-values-are-numbers/#findComment-1389955 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.