?

Log in

No account? Create an account

Previous Entry | Next Entry

cheap thrills

I don't know why, but adding notes to php.net is one of the biggest thrills for me, of late. I'm giving something back. I'm showing off. I'm putting myself up for rejection. So far, I'm just ignored. But I'm not giving up! First bit of code went up, well, if you were paying attention you saw it. I don't remember. Aha! Oct 17. I'm actually recording these things, now -- http://erif.org/code/php.php

Oops! I never posted about it, then. Silly me. Well, check out the link above if you want to see the other super-craziness =)

Today's code:


test for valid US phone number, and get it back formatted at the same time

  function getUSPhone($var) {
    // area code
    $US_PHONE_PREG ="/^(?:\+?1[\-\s]?)?(\(\d{3}\)|\d{3})[\-\s\.]?"; 
    // seven digits
    $US_PHONE_PREG.="(\d{3})[\-\.]?(\d{4})"; 
    // any extension
    $US_PHONE_PREG.="(?:\s?x|\s|\s?ext(?:\.|\s)?)?(\d*)?$/"; 
    if (!preg_match($US_PHONE_PREG,$var,$match)) {
      return false;
    } else {
      $tmp = "+1 ";
      if (substr($match[1],0,1) == "(") {
        $tmp.=$match[1];
      } else {
        $tmp.="(".$match[1].")";
      }
      $tmp.=" ".$match[2]."-".$match[3];
      if ($match[4] <> '') $tmp.=" x".$match[4];
      return $tmp;
    }
  }



usage:
  $phone = $_REQUEST["phone"];
  if (!($phone = getUSPhone($phone))) {
    //error gracefully :)
  }


Tags:

Comments

( 6 comments — Leave a comment )
kakubeki
Oct. 27th, 2005 09:54 am (UTC)
That's rahl purty.
kaolinfire
Oct. 27th, 2005 09:56 am (UTC)
thankee :P
mackatlaw
Oct. 27th, 2005 04:20 pm (UTC)
Hmm. I understood about none of that. But that's okay. :)

Mack
nibot
Oct. 28th, 2005 01:26 pm (UTC)
unrelated to this particular post -- "neuroscience of love"
i found this post quite interesting and you may too:

http://www.livejournal.com/users/uberdionysus/267831.html
(Anonymous)
Oct. 28th, 2005 01:34 pm (UTC)
Re: unrelated to this particular post -- "neuroscience of love"
thank you :)
kaolinfire
Oct. 28th, 2005 01:37 pm (UTC)
Re: unrelated to this particular post -- "neuroscience of love"
goddamned disabled cookies so I can use weather.com because they suck, tra la la. good morning.
( 6 comments — Leave a comment )

Latest Month

February 2016
S M T W T F S
 123456
78910111213
14151617181920
21222324252627
2829     

Tags

Powered by LiveJournal.com
Designed by chasethestars