phone.br.inc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Brazilian phone numbers.
  5. * (based on CCK Field for French phone numbers.)
  6. */
  7. define('PHONE_BR_REGEX', "/^(\+|0{2}|)?(55|0|)[\s.-]?((\(0?[1-9][0-9]\))|(0?[1-9][0-9]))[\s.-]?([1-9][0-9]{2,4})[\s.-]?([0-9]{4})$/");
  8. function phone_br_metadata() {
  9. return array(
  10. 'error' => '"%value" is not a valid Brazilian phone number<br>Brazilian phone numbers should contain only numbers and spaces and - and be like 099 9999-9999, 99 9999-9999 or 99 99999-9999 with an optional prefix of "+55".',
  11. );
  12. }
  13. /**
  14. * Verification for Brazilian Phone Numbers.
  15. *
  16. * @param string $phonenumber
  17. * @return boolean Returns boolean FALSE if the phone number is not valid.
  18. */
  19. function valid_br_phone_number($phonenumber) {
  20. $phonenumber = trim($phonenumber);
  21. /*
  22. $phonenumber = str_replace(array(' ','-','(',')'), '', $phonenumber);
  23. */
  24. return (bool) preg_match(PHONE_BR_REGEX, $phonenumber);
  25. }
  26. /**
  27. * Formatting for Brazilian Phone Numbers.
  28. *
  29. * @param string $phonenumber
  30. * @return string Returns a string containting the phone number with some formatting.
  31. */
  32. function format_br_phone_number($phonenumber, $field = FALSE) {
  33. $phone = str_replace(array(' ','-','(',')'), '', $phonenumber);
  34. if (preg_match(PHONE_BR_REGEX, $phone, $matches) != 1) {
  35. return $phonenumber; // this is possible?
  36. }
  37. $formatedphone = '';
  38. if ($field && $field['phone_country_code']) {
  39. $formatedphone .= '+55 ';
  40. }
  41. $formatedphone .= '(' . $matches[3] . ')';
  42. $formatedphone .= ' ' . $matches[6] . '-';
  43. $formatedphone .= '' . $matches[7];
  44. return $formatedphone;
  45. }