0

How can I check values in an array that has numbers containing only four digits? If there are strings or numbers longer than four digits, I want to echo an error message.

This is correct: $searchText = '3423, 2453, 3245 , 2425, 6765';

This is wrong: $searchText = '34d23, 244353, fsddf , 2d4425, 674365';

How can I do this?

I tried something like this...

$searchText = '3423,2453,3245,2425,6765'; 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; } } if(!validate($searchText)) echo 'fail'; else echo 'pass'; 

output is: fail why is it?

1
  • Have you tried anything? Commented Nov 3, 2012 at 5:59

4 Answers 4

3
$searchText = '3423, 2453, 3245 , 2425, 6765'; $vals = explode(",",$searchText); foreach($vals as $v){ $v = trim($v); if(is_numeric($v) && strlen($v)==4){ echo 'good to go<br>'; }elseif(is_numeric($v)){ echo 'num but not 4 digits<br>'; }else{ echo 'string<br>'; } } 
Sign up to request clarification or add additional context in comments.

2 Comments

is there a way to check before going to explode?
unfortunately, since it's a string, there won't be a way of telling unless you break the string down.
2

Those aren't arrays (they're strings), but you can handle the problem by splitting the string into an array and using PHP's filtering (filter_var in this case):

if(!validate($searchText)) // fail else // pass function validate($input) { $inputArray = array_map('trim', explode(',', $input)); foreach($inputArray as $item) { if(filter_var($item, FILTER_VALIDATE_INT) === false || strlen($item) != 4) return false; } return true; } 

4 Comments

Your code doesn't account for whitespace in the string, and delimiter is first param to explode, not second. Try $searchArray = array_map('trim', explode(',', $searchText)); instead
check I updated code with your solution.. but its not working.
@TharangaNuwanKumara Sorry, my fault. I've edited my answer with return true at the end of the function.
@TharangaNuwanKumara What do you mean? It's only working on $inputArray inside that function. It doesn't actually edit your $searchText variable.
1

You can use the below logic.(If you mean "How can I check values in an array that has numbers containing only four digits? ", then below is the solution)

<?php //you can use explode() function to convert your string into array. //$searchText = '3423, 2453, 3245 , 2425, 6765'; //$searchText = explode(",",$searchText); $searchText = array('3423', '2453', '3245' , '2425', '6765'); $count = sizeof($searchText); foreach($searchText as $key=>$value) { if (is_numeric($value) && preg_match('/^\d{4}$/', $value)) { // pass } else { $count--; // fail } } if($count == sizeof($searchText)) { echo "All good"; } ?> 

Comments

0

replace your array variable with $ary this is return number of characters is in array

$maxlen = max(array_map('strlen', $ary)); 

for better explanation StackOverflow

1 Comment

yes replace it will return number of characters in the certain array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.