StefanRSA Posted July 6, 2010 Share Posted July 6, 2010 I want to hide dif formats of telephone numbers in a string. Getting so confused with all the help out there.... What function can I use to hide any of the following phone formats in a string that can contain Text, symbols and numbers? (xxx) xxx-xxxx xxx xxx xxxx xxxxxxxxxx +xx xx xxx xxxx xxx - xxx xxxx Quote Link to comment https://forums.phpfreaks.com/topic/206839-str_replace-regex-hide-phone-number-formats/ Share on other sites More sharing options...
premiso Posted July 6, 2010 Share Posted July 6, 2010 You could just strip all non-numeric characters and then format it how you want. As long as you are always expecting 10, you can format it how you want it: $num = preg_replace('~[^0-9]+~', '', $phone_input); Then you can use substr to put it back how you want, or even use preg_replace again to format it how you want. IE: $formatted = preg_replace('~([0-9]){3}([0-9]){3}([0-9]){4}~', '($1) $2-$3', $num); Un-tested, but should give you a working theory / something to build off of. Quote Link to comment https://forums.phpfreaks.com/topic/206839-str_replace-regex-hide-phone-number-formats/#findComment-1081709 Share on other sites More sharing options...
StefanRSA Posted July 6, 2010 Author Share Posted July 6, 2010 Thanks for the reply premiso. Allow me to explain a bit more about my problem... I might get an input from a user that looks like this: $adtext='We also do custom design hair for cancer patience. 1. Coloring 2. Highlights 3. Cut To order today please call : (xxx) xxx xxxx / xxx xxx xxx'; I want to preg_replace any phone format in the $adtext string. The idea is to make sure people don't put phone numbers in the $adtext string as there is phone number fields that visitor need to click (Using AJAX) before they can view the number (Think its a clever way of tracking phone number requests on a website) Quote Link to comment https://forums.phpfreaks.com/topic/206839-str_replace-regex-hide-phone-number-formats/#findComment-1081720 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.