Check if conditions is right in array?

I have a str like this:
2,y,6,63,1,g,6,89,1,b,6,37,12,y,7,99

I then put this string into an array like this:

$string = '2,y,6,63,1,g,6,89,1,b,6,37,12,y,7,99';

$stringarray = explode(",",$string);
$h = '';
$k = 0;
foreach($stringarray as $value){
	$h .= ($k%4 == 3)?$value.';':$value.',';
	$k++;
}
$stringarray = explode(";",substr($h,0,-1));

This makes an array like this which looks perfect:

Array
(
    [0] => 2,y,6,63
    [1] => 1,g,6,89
    [2] => 1,b,6,37
    [3] => 12,y,7,99
)

Now, sometimes I get an string with a fault in it like this:
2,y,6,63,1,g,6,89,1,b,6,37,15,16,y,9,69

Now then the array comes out like so:

Array
(
    [0] => 2,y,6,63
    [1] => 1,g,6,89
    [2] => 1,b,6,37
    [3] => 15,,16,y
    [4] => 9,69
)

As you can see the array gets messed up and are no good… It should be [number, letter,number,number] and no spaces…

How can I check this and make an error if there is something wrong?

Thanks in advance :slight_smile:

There might be a more elegant approach, but I would just do it the direct way.


    foreach ($stringarray as $v)
    {
         $temp = explode(',', $v);
         if (ctype_digit($temp[0]) && ctype_alpha($temp[1]) && ctype_digit($temp[2] && ctype_digit($temp[3])) { # it passed }
         else { # failed }
    }

You could do it during the initial array building for efficiency and regex would be faster, but the above is a quick and direct approach