parsing phone numbers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    #1

    parsing phone numbers

    Hi,

    Using php 4.4.4, I am looking to parse a US phone number string into 3
    smaller strings -- area code, the first part of the phone number (3
    digits) and the last part of the phone number (4 digits). The only
    given is that there will be 10 digits in the string, but people may
    include spaces, parens, or other characters. These are all strings
    I'd expect

    $p = "5558675309 ";
    $p = "(312) 123-4567";
    $p = "512-234-5678";

    Thanks for any code snippets, - Dave

  • Roy Kaldung

    #2
    Re: parsing phone numbers

    laredotornado@z ipmail.com schrieb:
    Hi,
    >
    Using php 4.4.4, I am looking to parse a US phone number string into 3
    smaller strings -- area code, the first part of the phone number (3
    digits) and the last part of the phone number (4 digits). The only
    given is that there will be 10 digits in the string, but people may
    include spaces, parens, or other characters. These are all strings
    I'd expect
    >
    $p = "5558675309 ";
    $p = "(312) 123-4567";
    $p = "512-234-5678";
    >
    Thanks for any code snippets, - Dave
    >
    <?php
    $a = "(312) 123-4567";
    $input = str_replace(arr ay(' ','-',')','('), '', $a);
    if (preg_match('/(\d{3})(\d{3})( \d{4})/', $input, $output)) {
    var_dump($outpu t);
    } else {
    print "wrong format!";
    }
    ?>

    hth,
    Roy

    Comment

    • JackM

      #3
      Re: parsing phone numbers

      laredotornado@z ipmail.com wrote:
      Hi,
      >
      Using php 4.4.4, I am looking to parse a US phone number string into 3
      smaller strings -- area code, the first part of the phone number (3
      digits) and the last part of the phone number (4 digits). The only
      given is that there will be 10 digits in the string, but people may
      include spaces, parens, or other characters. These are all strings
      I'd expect
      >
      $p = "5558675309 ";
      $p = "(312) 123-4567";
      $p = "512-234-5678";
      This may not be the best option but it's what I sometimes use. You
      should be able to modify it for your purposes:

      <?php

      //process the phone number (USA only) and strip brackets and add dashes
      where needed. Output: 555-555-5555
      $phone_number = $_POST['phone'];
      $dash = "-";
      $ext = $_POST['ext'];
      $ext1 = " ext";

      $pattern = '/^[\(]?(\d{0,3})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{4})$/';
      if (preg_match($pa ttern, $phone_number, $matches)) {
      // we have a match, dump sub-patterns to $matches
      $phone_number = $matches[0]; // original number
      $area_code = $matches[1]; // 3-digit area code
      $exchange = $matches[2]; // 3-digit exchange
      $number = $matches[3]; // 4-digit number
      }
      if ($ext == "") {
      $phone = $area_code . $dash . $exchange . $dash . $number;
      }else{
      $phone = $area_code . $dash . $exchange . $dash . $number . $ext1 . $ext;
      }

      ?>

      Comment

      • Toby A Inkster

        #4
        Re: parsing phone numbers

        laredotornado@z ipmail.com wrote:
        Using php 4.4.4, I am looking to parse a US phone number string into 3
        smaller strings -- area code, the first part of the phone number (3
        digits) and the last part of the phone number (4 digits). The only
        given is that there will be 10 digits in the string, but people may
        include spaces, parens, or other characters.
        Are you trying to pull a phone number out of a longer sequence of text
        that may contain other numeric things too, or are you trying to parse it
        from a dedicated phone number field?

        If the former, the two examples already posted look OK. If the latter, then
        you can probably afford to be more forgiving. Here is some code that ought
        to be more flexible:

        <?php
        $phone = '2-45 67598 1...2';
        $parsed = parse_phone($ph one);
        print_r($parsed );

        function parse_phone ($string, $country='us')
        {
        if ($country != 'us')
        die("Not implemented yet!");
        $retval = array();

        $string = preg_replace('/[^0-9]/', '', $string);

        if (strlen($string )<10)
        $retval['Warning'] = "Phone number too short.";

        if (strlen($string )>10)
        $retval['Warning'] = "Phone number too long. "
        . "Possible extension included.";

        $retval['AREA'] = substr($string, 0, 3);
        $retval['START'] = substr($string, 3, 3);
        $retval['FINISH'] = substr($string, 6, 4);
        $retval['EXT'] = substr($string, 10);

        return $retval;
        }
        ?>

        --
        Toby A Inkster BSc (Hons) ARCS
        Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

        Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux

        Comment

        • Krustov

          #5
          Re: parsing phone numbers

          <comp.lang.ph p>
          <laredotornado@ zipmail.com>
          <7 May 2007 12:56:49 -0700>
          <1178567809.585 694.21720@y80g2 000hsf.googlegr oups.com>
          $p = "5558675309 ";
          $p = "(312) 123-4567";
          $p = "512-234-5678";
          >
          Thanks for any code snippets
          >
          What you could do is strip everything from the phone number string
          except the 0123456789 and then reform it to anything you want .

          The substr() stuff hasnt been checked .




          <?php

          $p="(312) 123-4567";

          $dummy=$p; include('the_fi le_below.php'); $p=$dummy;

          $p1=substr($p,0 ,3);
          $p2=substr($p,4 ,3);
          $p3=substr($p,9 ,4);

          $poop="(" . $p1 . ")" . " " . $p2 . "-" . $p3;

          ?>




          <?php

          $ok="1234567890 ";

          $zstring=strlen ($ok);

          $zdummy=strlen( $dummy);

          $jazz=0;
          while ($jazz<256)
          {
          $homer[$jazz]=0;
          $jazz=$jazz+1;
          }

          $jazz=0;
          while ($jazz<$zstring )
          {
          $junk=substr($o k,$jazz,1); $qaz=ord($junk) ; $homer[$qaz]=1;
          $jazz=$jazz+1;
          }

          $gimmie="";

          if ($zdummy>0)
          {

          $jazz=0;
          while ($jazz<$zdummy)
          {
          $junk=substr($d ummy,$jazz,1); $qaz=ord($junk) ;
          if ($homer[$qaz]==1) {$gimmie=$gimmi e . $junk;}
          $jazz=$jazz+1;
          }

          }

          $dummy=$gimmie;

          ?>


          Comment

          • Toby A Inkster

            #6
            Re: parsing phone numbers

            Krustov wrote:
            $jazz, $junk, $zdummy, $qaz, $zstring, $gimmie
            WTF?! This isn't Scrabble[TM]; you don't get bonus points for including Z,
            Q and J in your variable names.

            As far as I can tell, your entire 40-line include file can be replaced with:

            $p = preg_replace('/[^0-9]/', '', $p);

            which is only 40 characters.

            --
            Toby A Inkster BSc (Hons) ARCS
            Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

            Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux

            Comment

            • Krustov

              #7
              Re: parsing phone numbers

              <comp.lang.ph p>
              <Toby A Inkster>
              <Wed, 9 May 2007 09:53:03 +0100>
              <foe8h4-8oj.ln1@ophelia .g5n.co.uk>
              $jazz, $junk, $zdummy, $qaz, $zstring, $gimmie
              >
              WTF?! This isn't Scrabble[TM]; you don't get bonus points for including Z,
              Q and J in your variable names.
              >
              As far as I can tell, your entire 40-line include file can be replaced with:
              >
              $p = preg_replace('/[^0-9]/', '', $p);
              >
              which is only 40 characters.
              >
              The $ok string can contain any characters and its a lot easier and
              faster to duplicate/rename the file than it is to learn expressions .

              $toby seems like a good variable name .

              Comment

              • Toby A Inkster

                #8
                Re: parsing phone numbers

                Krustov wrote:
                >As far as I can tell, your entire 40-line include file can be replaced with:
                >$p = preg_replace('/[^0-9]/', '', $p);
                >which is only 40 characters.
                >
                The $ok string can contain any characters and its a lot easier and
                faster to duplicate/rename the file than it is to learn expressions .
                You think it's faster to duplicate, rename and modify that file than it is
                to change this:

                $p = preg_replace('/[^0-9]/', '', $p);

                to, say, this:

                $p = preg_replace('/[^AaEeIiOoUu]/', '', $p);

                ?!

                (That regular expression removes everything except vowels.)

                --
                Toby A Inkster BSc (Hons) ARCS
                Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

                Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux

                Comment

                Working...