Advertisement
Filkolev

01. Phone Numbers

Jan 12th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.97 KB | None | 0 0
  1. <?php
  2.  
  3. $numbersString = $_GET['numbersString'];
  4. $numbersString = preg_replace("/\s+/", "", $numbersString);
  5.  
  6. $pattern = "/([A-Z][a-zA-Z]*)(?:[^a-zA-Z+\d]+|\d?)*?(\+?\d*(?:[(\/\-. ]?\d+[) .\-\/]?)+)+/";
  7. preg_match_all($pattern, $numbersString, $matches, PREG_SET_ORDER);
  8. $result = array();
  9.  
  10. foreach ($matches as $match) {
  11.     $name = $match[1];
  12.  
  13.  
  14.     if (strlen(preg_replace("/[^\d+]+/", "", $match[2])) < 2) {
  15.         continue;
  16.     }
  17.  
  18.     $result[] = [$name, $match[2]];
  19. }
  20.  
  21. if (count($result) == 0 || (count($result) == 1 && (strpos($result[0][1], "(") === false))) {
  22.     echo "<p>No matches!</p>";
  23. } else {
  24.     $toPrint = "<ol>";
  25.     foreach ($result as $entry) {
  26.         $entry[1] = preg_replace("/[^\d+]+/", "", $entry[1]);
  27.  
  28.         if (preg_match("/(\+?[(]*\d+[\-) \/.]*)/", $entry[1]) == 0) {
  29.             while (true) {}
  30.         }
  31.         $toPrint .= "<li><b>". ($entry[0]) .":</b> ". ($entry[1]) ."</li>";
  32.     }
  33.  
  34.     $toPrint .= "</ol>";
  35. }
  36.  
  37. echo $toPrint;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement