Zend - The PHP Company




Text

Add Code


Phone-Number-Checker  

Type: code fragment
Added by: ThB
Entered: 13/06/2001
Last modified: 06/12/2000
Rating: - (fewer than 3 votes)
Views: 8055
Breaks a phone number string into country code, city code, number and dial trough. Takes many different formats.


<?php

   
#(c) Thomas Beckmann, Knowledge Network Kiel 2001

   # Recognizes phone-numbers
   # returns false if there are forbidden unrecognizable in the PhoneNumber or an array with the elements:
   # 'tel'  => formatted number in the form '+ctry(city)user-locl'
   # 'nr'   => number in the form countrycode 'ctry city user local' (w/o spaces, just digits)
   # 'ctry' => country (e.g. 1 for usa, 49 for germany)
   # 'city' => city w/o longdistcode
   # 'user' => users phonenumber
   # 'locl' => dial through
   # the parameters are use as defaults
   
function phone($PhoneNumber$countrycode='00'$longdistcode='0'$country='49'$city='431')
   {
      
# unerlaubte Zeichen drin ?
      
if (preg_match('~[^-sd./()+]~'$PhoneNumber)) return false;

      
# Normalisierungen:
      
$PhoneNumber trim($PhoneNumber);
      
# Tabs und mehrfache Leerschritte, mehrfache Trenner und Leerschritt-Trenner-Kombinationen weg
      
$PhoneNumber preg_replace('~s+~'' '$PhoneNumber);
      
$PhoneNumber preg_replace('~([-./()])+~''1'$PhoneNumber);
      
$PhoneNumber preg_replace('~([-./()]) ~''1'$PhoneNumber);
      
$PhoneNumber preg_replace('~ ([-./()])~''1'$PhoneNumber);

      
# Anfang normaliseren auf $countrycode
      
$PhoneNumber preg_replace('~^++ ?('.$countrycode.')?~'$countrycode$PhoneNumber);

      
$teil = array();
      if (
strstr($PhoneNumber'(')) # wenn (, ist Vorwahl eingeklammert -> Zerlegung in Land, Vorwahl und Nummer
      
{
         
preg_match('~^(.*)((.*))(.*)$~U'$PhoneNumber$teil);
         list(
$foo$tel['ctry'], $tel['city'], $tel['user']) = $teil;
         if (empty(
$tel['ctry'])) $tel['ctry'] = $country;
      }
      else if (
preg_match('~[/.]~'$PhoneNumber)) # / oder . trennt Vorwahl und Rufnr -> Zerlegung in Land+Vorwahl und Nummer
      
{
         
$teil preg_split('~[/.]~'$PhoneNumber);
         
$tel['city'] = $teil[0]; unset($teil[0]);
         
$tel['user'] = implode('-'$teil);
      }

      if (isset(
$tel))
      {
         
# aus der Vorwahl das Land rausholen
         
if (preg_match('~^'.$countrycode.'~'$tel['city']) && (strstr($tel['city'], '-') || strstr($tel['city'], ' ')))
         {
            
$teil preg_split('~[- ]~'$tel['city']);
            if (
$teil[0] == $countrycode# nach dem Auslandszeichen/-code ist ein Trenner gewesen
            
{
               
$tel['ctry'] = $teil[0].$teil[1]; unset($teil[0]); unset($teil[1]);
               
$tel['city'] = implode(''$teil);
            }
            else
            {
               
$tel['ctry'] = $teil[0]; unset($teil[0]);
               
$tel['city'] = implode(''$teil);
            }
         }

         
# aus der Teilnehmernummer die Durchwahl rausholen
         
if (strstr($tel['user'], '-'))
         {
            
$teil preg_split('~-~'$tel['user']);
            
$i count($teil)-1;
            
$tel['locl'] = $teil[$i]; unset($teil[$i]);
            
$tel['user'] = implode(''$teil);
         }

         if (empty(
$tel['city'])) $tel['city'] = $city;
      }
      else
      {
         if (
strstr($PhoneNumber'-') || strstr($PhoneNumber' '))
         {
            
$teil preg_split('~[- ]~'$PhoneNumber);

            if (isset(
$teil[0]) && preg_match('~^'.$countrycode.'~'$teil[0])) # es gibt eine Auslandskennziffer
            
{
               if (
$teil[0] == $countrycode# nach dem Auslandszeichen/-code ist ein Trenner gewesen
               
{
                  
$tel['ctry'] = $teil[0].$teil[1]; unset($teil[0]); unset($teil[1]);
               }
               else
               {
                  
$tel['ctry'] = $teil[0]; unset($teil[0]);
               }
               
$teil explode('-'implode('-'$teil));
            }
            if (isset(
$teil[0]) && preg_match('~^'.$longdistcode.'~'$teil[0])) # es gibt eine Nicht-Ortsnetznummer
            
{
               
$tel['ctry'] = $countrycode;
               if (
$teil[0] == $longdistcode# nach dem Ortsnetzcode ist ein Trenner gewesen
               
{
                  
$tel['city'] = $teil[0].$teil[1]; unset($teil[0]); unset($teil[1]);
               }
               else
               {
                  
$tel['city'] = $teil[0]; unset($teil[0]);
               }
            }
            else if (isset(
$teil[0]) && isset($tel['ctry'])) # es gibt eines Landeskennung und die Orts-Vorwahl wurde nicht extra hervorgehoben
            
{
               
$tel['city'] = $teil[0]; unset($teil[0]);
            }

            
$tel['user'] = implode('-'$teil);
            
# aus der Teilnehmernummer die Durchwahl rausholen
            
if (strstr($tel['user'], '-'))
            {
               
$teil preg_split('~-~'$tel['user']);
               
$i count($teil)-1;
               
$tel['locl'] = $teil[$i]; unset($teil[$i]);
               
$tel['user'] = implode(''$teil);
            }
         }
         else
         {
            
$tel['user'] = preg_replace('~[^0-9]~'''$PhoneNumber);
            if (!
preg_match('~^'.$countrycode.'~'$PhoneNumber)) # es gibt keine Auslands-Kennziffer
            
{
               
$tel['ctry'] = $country;
               if (!
preg_match('~^'.$longdistcode.'~'$PhoneNumber)) # es gibt keine Nicht-Ortsnetznummer
                  
$tel['city'] = $city;
            }
         }

      }

      foreach (
$tel as $key=>$val$tel[$key] = preg_replace('~D~',''$val);
      if (!empty(
$tel['ctry']))
      {
         
$tel['ctry'] = preg_replace('~^'.$countrycode.'~',  ''$tel['ctry']);
      }
      if (!empty(
$tel['city']))
      {
         
$tel['city'] = preg_replace('~^'.$longdistcode.'~'''$tel['city']);
         if (empty(
$tel['ctry'])) $tel['ctry'] = $country;
      }
      else
      {
         
$tel['ctry'] = $country;
         
$tel['city'] = $city;
      }

      
$tel['tel'] = (empty($tel['ctry']) ? '' '+'.$tel['ctry']).
                    (empty(
$tel['city']) ? '' '('.$tel['city'].')').
                    (empty(
$tel['user']) ? '' $tel['user']).
                    (empty(
$tel['locl']) ? '' '-'.$tel['locl']);
      
$tel['nr']  = (empty($tel['ctry']) ? '' $countrycode.$tel['ctry']).
                    (empty(
$tel['city']) ? '' : (empty($tel['ctry']) ? $longdistcode '').$tel['city']).
                    (empty(
$tel['user']) ? '' $tel['user']).
                    (empty(
$tel['locl']) ? '' $tel['locl']);
      return 
$tel;
   }

?>


Usage Example


   $PhoneNumber = phone('181 25');
   echo 'Nummer: "'.$PhoneNumber['tel'].'" +'.$PhoneNumber['ctry'].'( '.$PhoneNumber['city'].')'.$PhoneNumber['user'].'-'.$PhoneNumber['locl'].' '."<br>n";
   $PhoneNumber = phone('0431/18125');
   echo 'Nummer: "'.$PhoneNumber['tel'].'" +'.$PhoneNumber['ctry'].'( '.$PhoneNumber['city'].')'.$PhoneNumber['user'].'-'.$PhoneNumber['locl'].' '."<br>n";
   $PhoneNumber = phone('+49-431-18125');
   echo 'Nummer: "'.$PhoneNumber['tel'].'" +'.$PhoneNumber['ctry'].'( '.$PhoneNumber['city'].')'.$PhoneNumber['user'].'-'.$PhoneNumber['locl'].' '."<br>n";
   $PhoneNumber = phone('+49(431)18125');
   echo 'Nummer: "'.$PhoneNumber['tel'].'" +'.$PhoneNumber['ctry'].'( '.$PhoneNumber['city'].')'.$PhoneNumber['user'].'-'.$PhoneNumber['locl'].' '."<br>n";
   $PhoneNumber = phone('+49-431-1812-5');
   echo 'Nummer: "'.$PhoneNumber['tel'].'" +'.$PhoneNumber['ctry'].'( '.$PhoneNumber['city'].')'.$PhoneNumber['user'].'-'.$PhoneNumber['locl'].' '."<br>n";
   $PhoneNumber = phone('+49 (0431) 1812-5');
   echo 'Nummer: "'.$PhoneNumber['tel'].'" +'.$PhoneNumber['ctry'].'( '.$PhoneNumber['city'].')'.$PhoneNumber['user'].'-'.$PhoneNumber['locl'].' '."<br>n";

Leads to 

Nummer: "+49(431)181-25" +49( 431)181-25 
Nummer: "+49(431)18125" +49( 431)18125- 
Nummer: "+49(431)18125" +49( 431)18125- 
Nummer: "+49(431)18125" +49( 431)18125- 
Nummer: "+49(431)1812-5" +49( 431)1812-5 
Nummer: "+49(431)1812-5" +49( 431)1812-5 


Rate This Script





Search



This Category All Categories