{"id":782,"date":"2010-01-12T07:16:31","date_gmt":"2010-01-12T14:16:31","guid":{"rendered":"http:\/\/jamesg.net\/?p=782"},"modified":"2011-03-29T13:26:38","modified_gmt":"2011-03-29T17:26:38","slug":"formatting-phone-number-and-fixing-user-input-with-php","status":"publish","type":"post","link":"https:\/\/jamesg.net\/?p=782","title":{"rendered":"Formatting Phone Number and Fixing User Input With PHP"},"content":{"rendered":"<p>Recently I was writing a franchise module for a shopping cart software that required franchise owners to input their phone number.\u00a0 I&#8217;ve ran into the problem where no matter what instructions I&#8217;ve given, the franchise owners consistently like to format the phone number in their own unique way.\u00a0 Rather than add 3 text boxes to where they have to tab between every part of the phone number, I wrote a quick and easy php script to handle all the leg work for me!<\/p>\n<p>[php]<br \/>\n\/\/Input example<br \/>\n\/\/(999)999.9999<br \/>\n\/\/Output examples<br \/>\n\/\/formatPhoneNum($phone_number, 1) outputs (999) 999-9999<br \/>\n\/\/ Inputting 999-9999 would cause the function to return &#8220;False&#8221;<br \/>\nfunction formatPhoneNum($phone_number, $style){<br \/>\n\/\/ Style 1 &#8211; (999) 999-9999<br \/>\n\/\/ Style 2 &#8211; 999-999-9999<br \/>\n\/\/ Style 3 &#8211; 999.999.9999<br \/>\n\/\/ Style 4 &#8211; 9999999999<br \/>\n$number = str_replace(array(&#8216;(&#8216;, &#8216;)&#8217;, &#8216;-&#8216;, &#8216; &#8216;), &#8221;, $phone_number);<br \/>\nif (strlen($number) == 10){<br \/>\n$area = substr($number, 0, 3);<br \/>\n$first = substr($number, 3, 3);<br \/>\n$last = substr($number, 6);<\/p>\n<p>switch($style){<br \/>\ncase 1:<br \/>\nreturn &#8220;($area) $first-$last&#8221;;<br \/>\ncase 2:<br \/>\nreturn &#8220;$area-$first-$last&#8221;;<br \/>\ncase 3:<br \/>\nreturn &#8220;$area.$first.$last&#8221;;<br \/>\ncase 4:<br \/>\nreturn &#8220;$area$first$last&#8221;;<br \/>\n}<br \/>\n\/\/ if the phone number was too long or too short<br \/>\nelse {<br \/>\nreturn false;<br \/>\n}<br \/>\n}<br \/>\n[\/php]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I was writing a franchise module for a shopping cart software that required franchise owners to input their phone number.\u00a0 I&#8217;ve ran into the problem where no matter what instructions I&#8217;ve given, the franchise owners consistently like to format the phone number in their own unique way.\u00a0 Rather than add 3 text boxes to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[1],"tags":[157,264],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/jamesg.net\/index.php?rest_route=\/wp\/v2\/posts\/782"}],"collection":[{"href":"https:\/\/jamesg.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jamesg.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jamesg.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jamesg.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=782"}],"version-history":[{"count":16,"href":"https:\/\/jamesg.net\/index.php?rest_route=\/wp\/v2\/posts\/782\/revisions"}],"predecessor-version":[{"id":785,"href":"https:\/\/jamesg.net\/index.php?rest_route=\/wp\/v2\/posts\/782\/revisions\/785"}],"wp:attachment":[{"href":"https:\/\/jamesg.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jamesg.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jamesg.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}