Skip to content

Validate Multiple Phone numbers Comma Separated with PHP?

    $str = "9703132428,123456789,1";
    // convert string to array         
    $numbers = explode(',', $str);
    // remove empty items from array
    $numbers = array_filter($numbers);
    // trim all the items in array 
    $numbers =array_map('trim', $numbers);
    
    // default error count
    $error = 0;
    // array to store invalid numbers
    $inValidNumbers = array();

    // loop through all the numbers in array  
    foreach($numbers as $number) {
      // number validation we allow only 0 to 9 , min 5 max 14 number only
    	if(!preg_match("/^[0-9,]{5,14}$/", $number)) {
              $error++; // increment error count
              // push the invalid numbers into array
              array_push($inValidNumbers,$number); 

    	}
    }

    if($error != 0) { 
       'invalid numbers : '. implode(", ", $inValidNumbers);
    } else {
       // do something...
    }
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments