{"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":{"footnotes":""},"categories":[1],"tags":[157,264],"class_list":["post-782","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-php-code","tag-programming"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.10 - aioseo.com -->\n\t<meta name=\"description\" content=\"Recently I was writing a franchise module for a shopping cart software that required franchise owners to input their phone number. I&#039;ve ran into the problem where no matter what instructions I&#039;ve given, the franchise owners consistently like to format the phone number in their own unique way. Rather than add 3 text boxes to\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"James Garrett\"\/>\n\t<meta name=\"keywords\" content=\"php code,programming,uncategorized\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/jamesg.net\/?p=782\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.10\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"James Garrett&#039;s Blog | Software developer passionate about all things computer science, nature, and travel\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Formatting Phone Number and Fixing User Input With PHP | James Garrett&#039;s Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Recently I was writing a franchise module for a shopping cart software that required franchise owners to input their phone number. I&#039;ve ran into the problem where no matter what instructions I&#039;ve given, the franchise owners consistently like to format the phone number in their own unique way. Rather than add 3 text boxes to\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/jamesg.net\/?p=782\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2010-01-12T14:16:31+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2011-03-29T17:26:38+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Formatting Phone Number and Fixing User Input With PHP | James Garrett&#039;s Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Recently I was writing a franchise module for a shopping cart software that required franchise owners to input their phone number. I&#039;ve ran into the problem where no matter what instructions I&#039;ve given, the franchise owners consistently like to format the phone number in their own unique way. Rather than add 3 text boxes to\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jamesg.net\\\/?p=782#article\",\"name\":\"Formatting Phone Number and Fixing User Input With PHP | James Garrett's Blog\",\"headline\":\"Formatting Phone Number and Fixing User Input With PHP\",\"author\":{\"@id\":\"https:\\\/\\\/jamesg.net\\\/?author=1#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/jamesg.net\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/jamesg.net\\\/?p=782#articleImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dcc0cae12f663ca123ac5342af5dbcf38c469405e0fb3d5f59984199cdb04243?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"James Garrett\"},\"datePublished\":\"2010-01-12T07:16:31-05:00\",\"dateModified\":\"2011-03-29T13:26:38-04:00\",\"inLanguage\":\"en-US\",\"commentCount\":2,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jamesg.net\\\/?p=782#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/jamesg.net\\\/?p=782#webpage\"},\"articleSection\":\"Uncategorized, PHP Code, Programming\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jamesg.net\\\/?p=782#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/jamesg.net#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jamesg.net\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/jamesg.net\\\/?cat=1#listItem\",\"name\":\"Uncategorized\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/jamesg.net\\\/?cat=1#listItem\",\"position\":2,\"name\":\"Uncategorized\",\"item\":\"https:\\\/\\\/jamesg.net\\\/?cat=1\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/jamesg.net\\\/?p=782#listItem\",\"name\":\"Formatting Phone Number and Fixing User Input With PHP\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/jamesg.net#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/jamesg.net\\\/?p=782#listItem\",\"position\":3,\"name\":\"Formatting Phone Number and Fixing User Input With PHP\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/jamesg.net\\\/?cat=1#listItem\",\"name\":\"Uncategorized\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jamesg.net\\\/#person\",\"name\":\"James Garrett\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/jamesg.net\\\/?p=782#personImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dcc0cae12f663ca123ac5342af5dbcf38c469405e0fb3d5f59984199cdb04243?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"James Garrett\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jamesg.net\\\/?author=1#author\",\"url\":\"https:\\\/\\\/jamesg.net\\\/?author=1\",\"name\":\"James Garrett\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/jamesg.net\\\/?p=782#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dcc0cae12f663ca123ac5342af5dbcf38c469405e0fb3d5f59984199cdb04243?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"James Garrett\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jamesg.net\\\/?p=782#webpage\",\"url\":\"https:\\\/\\\/jamesg.net\\\/?p=782\",\"name\":\"Formatting Phone Number and Fixing User Input With PHP | James Garrett's Blog\",\"description\":\"Recently I was writing a franchise module for a shopping cart software that required franchise owners to input their phone number. I've ran into the problem where no matter what instructions I've given, the franchise owners consistently like to format the phone number in their own unique way. Rather than add 3 text boxes to\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jamesg.net\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jamesg.net\\\/?p=782#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/jamesg.net\\\/?author=1#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/jamesg.net\\\/?author=1#author\"},\"datePublished\":\"2010-01-12T07:16:31-05:00\",\"dateModified\":\"2011-03-29T13:26:38-04:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jamesg.net\\\/#website\",\"url\":\"https:\\\/\\\/jamesg.net\\\/\",\"name\":\"James Garrett's Blog\",\"description\":\"Software developer passionate about all things computer science, nature, and travel\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/jamesg.net\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Formatting Phone Number and Fixing User Input With PHP | James Garrett's Blog","description":"Recently I was writing a franchise module for a shopping cart software that required franchise owners to input their phone number. I've ran into the problem where no matter what instructions I've given, the franchise owners consistently like to format the phone number in their own unique way. Rather than add 3 text boxes to","canonical_url":"https:\/\/jamesg.net\/?p=782","robots":"max-image-preview:large","keywords":"php code,programming,uncategorized","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jamesg.net\/?p=782#article","name":"Formatting Phone Number and Fixing User Input With PHP | James Garrett's Blog","headline":"Formatting Phone Number and Fixing User Input With PHP","author":{"@id":"https:\/\/jamesg.net\/?author=1#author"},"publisher":{"@id":"https:\/\/jamesg.net\/#person"},"image":{"@type":"ImageObject","@id":"https:\/\/jamesg.net\/?p=782#articleImage","url":"https:\/\/secure.gravatar.com\/avatar\/dcc0cae12f663ca123ac5342af5dbcf38c469405e0fb3d5f59984199cdb04243?s=96&d=mm&r=g","width":96,"height":96,"caption":"James Garrett"},"datePublished":"2010-01-12T07:16:31-05:00","dateModified":"2011-03-29T13:26:38-04:00","inLanguage":"en-US","commentCount":2,"mainEntityOfPage":{"@id":"https:\/\/jamesg.net\/?p=782#webpage"},"isPartOf":{"@id":"https:\/\/jamesg.net\/?p=782#webpage"},"articleSection":"Uncategorized, PHP Code, Programming"},{"@type":"BreadcrumbList","@id":"https:\/\/jamesg.net\/?p=782#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/jamesg.net#listItem","position":1,"name":"Home","item":"https:\/\/jamesg.net","nextItem":{"@type":"ListItem","@id":"https:\/\/jamesg.net\/?cat=1#listItem","name":"Uncategorized"}},{"@type":"ListItem","@id":"https:\/\/jamesg.net\/?cat=1#listItem","position":2,"name":"Uncategorized","item":"https:\/\/jamesg.net\/?cat=1","nextItem":{"@type":"ListItem","@id":"https:\/\/jamesg.net\/?p=782#listItem","name":"Formatting Phone Number and Fixing User Input With PHP"},"previousItem":{"@type":"ListItem","@id":"https:\/\/jamesg.net#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/jamesg.net\/?p=782#listItem","position":3,"name":"Formatting Phone Number and Fixing User Input With PHP","previousItem":{"@type":"ListItem","@id":"https:\/\/jamesg.net\/?cat=1#listItem","name":"Uncategorized"}}]},{"@type":"Person","@id":"https:\/\/jamesg.net\/#person","name":"James Garrett","image":{"@type":"ImageObject","@id":"https:\/\/jamesg.net\/?p=782#personImage","url":"https:\/\/secure.gravatar.com\/avatar\/dcc0cae12f663ca123ac5342af5dbcf38c469405e0fb3d5f59984199cdb04243?s=96&d=mm&r=g","width":96,"height":96,"caption":"James Garrett"}},{"@type":"Person","@id":"https:\/\/jamesg.net\/?author=1#author","url":"https:\/\/jamesg.net\/?author=1","name":"James Garrett","image":{"@type":"ImageObject","@id":"https:\/\/jamesg.net\/?p=782#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/dcc0cae12f663ca123ac5342af5dbcf38c469405e0fb3d5f59984199cdb04243?s=96&d=mm&r=g","width":96,"height":96,"caption":"James Garrett"}},{"@type":"WebPage","@id":"https:\/\/jamesg.net\/?p=782#webpage","url":"https:\/\/jamesg.net\/?p=782","name":"Formatting Phone Number and Fixing User Input With PHP | James Garrett's Blog","description":"Recently I was writing a franchise module for a shopping cart software that required franchise owners to input their phone number. I've ran into the problem where no matter what instructions I've given, the franchise owners consistently like to format the phone number in their own unique way. Rather than add 3 text boxes to","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/jamesg.net\/#website"},"breadcrumb":{"@id":"https:\/\/jamesg.net\/?p=782#breadcrumblist"},"author":{"@id":"https:\/\/jamesg.net\/?author=1#author"},"creator":{"@id":"https:\/\/jamesg.net\/?author=1#author"},"datePublished":"2010-01-12T07:16:31-05:00","dateModified":"2011-03-29T13:26:38-04:00"},{"@type":"WebSite","@id":"https:\/\/jamesg.net\/#website","url":"https:\/\/jamesg.net\/","name":"James Garrett's Blog","description":"Software developer passionate about all things computer science, nature, and travel","inLanguage":"en-US","publisher":{"@id":"https:\/\/jamesg.net\/#person"}}]},"og:locale":"en_US","og:site_name":"James Garrett's Blog | Software developer passionate about all things computer science, nature, and travel","og:type":"article","og:title":"Formatting Phone Number and Fixing User Input With PHP | James Garrett's Blog","og:description":"Recently I was writing a franchise module for a shopping cart software that required franchise owners to input their phone number. I've ran into the problem where no matter what instructions I've given, the franchise owners consistently like to format the phone number in their own unique way. Rather than add 3 text boxes to","og:url":"https:\/\/jamesg.net\/?p=782","article:published_time":"2010-01-12T14:16:31+00:00","article:modified_time":"2011-03-29T17:26:38+00:00","twitter:card":"summary","twitter:title":"Formatting Phone Number and Fixing User Input With PHP | James Garrett's Blog","twitter:description":"Recently I was writing a franchise module for a shopping cart software that required franchise owners to input their phone number. I've ran into the problem where no matter what instructions I've given, the franchise owners consistently like to format the phone number in their own unique way. Rather than add 3 text boxes to"},"aioseo_meta_data":{"post_id":"782","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[],"defaultGraph":"","defaultPostTypeGraph":""},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2020-12-24 13:43:46","updated":"2025-06-07 03:05:37","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/jamesg.net\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/jamesg.net\/?cat=1\" title=\"Uncategorized\">Uncategorized<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tFormatting Phone Number and Fixing User Input With PHP\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/jamesg.net"},{"label":"Uncategorized","link":"https:\/\/jamesg.net\/?cat=1"},{"label":"Formatting Phone Number and Fixing User Input With PHP","link":"https:\/\/jamesg.net\/?p=782"}],"_links":{"self":[{"href":"https:\/\/jamesg.net\/index.php?rest_route=\/wp\/v2\/posts\/782","targetHints":{"allow":["GET"]}}],"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}]}}