Jump to content

str_replace & Regex - hide phone number formats


StefanRSA

Recommended Posts

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

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.

:shy:

 

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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.