Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple phone number formatting

Status
Not open for further replies.

jonblack

Programmer
Nov 25, 2008
11
0
0
US
I am looking for a simple function to take a string containing a phone number, strip non numeric out and return a standard output string.

ex. (555) 345-3545 = (555)345-3535
ex. 555-345-3545 = (555)345-3535
ex. 5553453545 = (555)345-3535

I could write this myself but why reinvent the wheel.
Jon B
 
func formatPhone(lcPhoneNo)
do case
case len(alltrim(lcPhoneNo)) = 14
return substr(lcPhoneNo,1,5)+substr(lcPhoneNo,7)
case len(alltrim(lcPhoneNo)) = 12
return "("+substr(lcPhoneNo,1,3)+")"+substr(lcPhoneNo,5)
case len(alltrim(lcPhoneNo)) = 10
return "("+substr(lcPhoneNo,1,3)+")"+substr(lcPhoneNo,4,3)+"-"+substr(lcPhoneNo,7,4))
otherwise
return lcPhoneNo
endcase
endfunc

didn't test it :), but, that should do that trick.



Ali Koumaiha
TeknoPCS Inc.
Dearborn heights, MI 48127
 
"I could write this myself but why reinvent the wheel."
Shhh - don't tell anyone, but many, many times we are 'inventing' it here and now too.
And, since it is so simple, we wouldn't want to eliminate an opportunity for you to explore your own creativity.

Regardless, something like below should take a phone number in any format (at least those I can think of) and return it as described above.

Code:
cOrigPhone = "345-123-4567"  && Or any other format
<whatever>
cNewPhone = FmtPhoneNo(cOrigPhone)  && Call Re-Format Function
<follow with whatever>

* ------------------------
FUNCTION FmtPhoneNo
PARAMETER cPhone
cPhone = STRTRAN(cPhone,"(","")  && Remove Left Parenthesis
cPhone = STRTRAN(cPhone,")","")  && Remove Right Parenthesis
cPhone = STRTRAN(cPhone," ","")  && Remove Spaces
cPhone = STRTRAN(cPhone,"-","")  && Remove Dashes
cPhone = STRTRAN(cPhone,".","")  && Remove Periods
* --- Phone No Should Be ONLY Numerics Now ---
cPhone = "(" + LEFT(cPhone,3) + ")"+SUBSTR(cPhone,4,3)+"-"+SUBSTR(cPhone,7,4)
RETURN cPhone

If I have missed any other possible phone number formats that I cannot think of, merely add and/or modify the appropriate code to handle it.

Good Luck
JRB-Bldr
 
I expect this will be one of those threads where a dozen different people will answer, and you'll get a dozen different suggestions - all of which will be equally valid.

So, for what it's worth, here's how I would do it:

Code:
lcNum = <the input number>
lcX = CHRTRAN(lcNum, "0123456789", "") 
lcY = CHRTRAN(lcNum, lcX, "")   && remove unwanted chars
lcResult = "(" + LEFT(lcY, 3) + ")" + SUBSTR(lcY, 4, 3) + ;
       "-" + RIGHT(lcY, 4)

By the way, you don't actually define what you mean by "a standard output string". I'm assuming you want a string that follows the pattern in your example.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
 

I would do it similar to Mike:

[blue][tt]
lcNum = <the input number>
lcX = CHRTRAN(lcNum, "0123456789", "") && find unwanted characters
lcY = CHRTRAN(lcNum, lcX, "") && remove unwanted characters
lcY = STUFF(lcY, 7, 0, "-") && insert dash
lcResult = "(" + STUFF(lcY, 4, 0, ")") && add parentheses
[/tt][/blue]


mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
And I have a very different suggestion. Don't format the phone numbers. Although, they are most of the time made up of digits, they are not numbers. What if as an enduser I want to store:

1-800-SPEEDYY

removing letters I wouldn't have something left useful. Users are wise enough to write a phone 'number' as they see fit.



Cetin Basoz
MS Foxpro MVP, MCP
 
Users are wise enough to write a phone 'number' as they see fit.

That's a very good point. Why should we impose our idea of the "standard format" on the user? Even if the phone number was being used by some other process (such as an auto-dialler), then it should be up to that process to make sense of it.

Jon, I would add that your "standard format" isn't the one I would choose. I write my own number as 0131-123 4567, or as +44 131 123 4567 as an international number (that's not my real number, by the way).

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Group, thanks for all the feedback. I was able to create the function I needed using all of the information toy provided. This saved me a ton of trial and error.

This is why these forums are great!
Jon
 
 
Jon, I would add that your "standard format" isn't the one I would choose. I write my own number as 0131-123 4567, or as +44 131 123 4567 as an international number (that's not my real number, by the way).

"(xxx) xxx-xxxx" has been the "standard" in the U.S. for decades, ever since direct dial was activated in the 1950's. However, since the Area Code is not optional in large parts of the U.S. any more, that format is slowly changing to "xxx-xxx-xxxx" especially in those areas that have overlapping Area Codes.

As for letting the users write as they see fit I don't see where Jon is preventing that. From what I see it looks like Jon is "standardizing" the format for some computer process, like auto-dialing (noted above), indexing, searching, etc. For all we know he could be just creating a second behind-the-scenes field that the user never sees and never uses.


mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
Mike is correct, JonBlack could be in some non-US country.

"(xxx) xxx-xxxx" has been the "standard" in the U.S. for decades

Based having been receiving lists of telephone numbers from clients, as well as state and federal regulatory agencies for a number of years now, I can confirm that "(xxx) xxx-xxxx" is NOT the "standard" for the U.S.

And it is certainly not the "standard" for importing into computer systems which perform automated dialing.

However it is one of the many familiar U.S. formats.

that format is slowly changing to "xxx-xxx-xxxx"

Actually, based on the number of lists received with it and the number of systems utilizing it, the "xxx-xxx-xxxx" format could very easily be considered by many people to have been the "standard" in the U.S. for many years now.

Regardless, while it might be a nice topic for debate - as to JonBlack's original question, he has the answer he was looking for and can easily modify it if he should want to use a different "standard" for whatever country he is in.

Good Luck,
JRB-Bldr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top