I have three id numbers $id = 'AA-2365' $id = 'A1-2365' $id = 'AA-2&65'
The id always starts with two letters and should be stored as $prefix followed by a separator and then an integer number which should be stored as $suffix. The separator is allowed to be a dash, forward slash or dot. I need to create a php function that accepts an id string as an argument. If the id is valid, the function should return an array containing the prefix and suffix values. If it is not valid, the function should return FALSE. I need to Create an array containing the three ids above. Now create a foreach loop that passes each id to your validation function and echoes a message stating whether the id is valid or not
I tried
function validator($id1,$id2,$id3){
$to_replace = array('.','/');
$clean = str_replace($to_replace, '-', $id1,$id2,$id3);
$split = explode('-', $clean);
$prefix = $split[0];
$suffix = $split[1];
foreach ($idmain as $value) {
if (ctype_alpha($prefix) && ctype_digit($suffix)) {
$valid ="id =$id is valid";
} else {
$valid = "id =$id is not valid";
}
return $valid;
}
but it does not work and im stuck any ideas please. i need help been at it all day
Thanks in advance
/^[a-z]{2}[\-\/.]\d{4}$/i
. – HamZa May 8 '13 at 22:21$id = 'A1-2365'
starts with A1, while you state the prefix is to be 2 letters. – nl-x May 8 '13 at 23:12AA-2&65
. – HamZa May 8 '13 at 23:31