Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
Will you explain specifically how the code above does not work? Additionally, will you update your question to show the output that you desire. How do you plan to structure the result array? –  George Cummins May 8 '13 at 22:20
1  
So basically using regex should do the job /^[a-z]{2}[\-\/.]\d{4}$/i. –  HamZa May 8 '13 at 22:21
    
Your second example id $id = 'A1-2365' starts with A1, while you state the prefix is to be 2 letters. –  nl-x May 8 '13 at 23:12
    
@nl-x I would think that that ID won't pass through the validator, the same goes for AA-2&65. –  HamZa May 8 '13 at 23:31
    
@HamZaDzCyberDeV i didnt even notice that one. ok, makes sense –  nl-x May 8 '13 at 23:33
add comment

1 Answer

try changing

foreach ($idmain as $value) {
  if (ctype............

into

for ($i = 0; $i < 3; $i++) {
  $value = $("id" . $i);
  if (ctype............

so you can loop through $id1, $id2 and $id3. Because now I really have no clue what $idmain is supposed to mean.

share|improve this answer
    
Sorry if i wasnt clear. Its a university question that has to be answered in that particular way and im stuck. It must follow the steps listed above –  leelnj May 9 '13 at 6:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.