472,799 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,799 software developers and data experts.

phone number formatting

this is probably a stupid question so apologies in advance.

I am trying to format a number to look like a phone number with "-"'s
between the numbers etc e.g. 15554256987 should be formatted as
1-555-425-6987.

Thanks
Jul 17 '05 #1
3 9846
Shay Hurley wrote:
this is probably a stupid question so apologies in advance.

I am trying to format a number to look like a phone number with "-"'s
between the numbers etc e.g. 15554256987 should be formatted as
1-555-425-6987.

What have you tried?
What did you expect and what did your script do?

Are you sure all your numbers have exactly 11 digits?

Here's a 'brute force' method to insert dashes at the positions you
specified. You might want to turn it into a function ... and change the
positions (and quantity) of dashes to insert.

#v+
<?php
$number = '15554256987';

$number = substr($number, 0, 1) . '-' . substr($number, 1);
// now $number = '1-5554256987';

$number = substr($number, 0, 5) . '-' . substr($number, 5);
// now $number = '1-555-4256987';

$number = substr($number, 0, 9) . '-' . substr($number, 9);
// now $number = '1-555-425-6987';
?>
#v-
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
Like Pedro said, a few problems...
what if I enter (555) 555-1212, or (555)-333-3321 or +1 555 333 1222 or, if
I'm from germany... 011 49 030 30 30 90 (phone number of my favorite hotel
in Berlin...)

Pedro Graca wrote:
Shay Hurley wrote:
this is probably a stupid question so apologies in advance.

I am trying to format a number to look like a phone number with "-"'s
between the numbers etc e.g. 15554256987 should be formatted as
1-555-425-6987.

What have you tried?
What did you expect and what did your script do?

Are you sure all your numbers have exactly 11 digits?

Here's a 'brute force' method to insert dashes at the positions you
specified. You might want to turn it into a function ... and change
the positions (and quantity) of dashes to insert.

#v+
<?php
$number = '15554256987';

$number = substr($number, 0, 1) . '-' . substr($number, 1);
// now $number = '1-5554256987';

$number = substr($number, 0, 5) . '-' . substr($number, 5);
// now $number = '1-555-4256987';

$number = substr($number, 0, 9) . '-' . substr($number, 9);
// now $number = '1-555-425-6987';

#v-

Jul 17 '05 #3
Sorry, I should have been clearer in my post. I am getting a list of
numbers of different lengths, could be anything from 5 digits (directory
enquiries) to local numbers (8 digits) to international numbers (12 or
13 numbers). There is no foratting on the numbers I receive from the db
so they are just like 14567890, 11811, 15552345678, 447878787712.
Agelmar wrote:
Like Pedro said, a few problems...
what if I enter (555) 555-1212, or (555)-333-3321 or +1 555 333 1222 or, if
I'm from germany... 011 49 030 30 30 90 (phone number of my favorite hotel
in Berlin...)

Pedro Graca wrote:
Shay Hurley wrote:
this is probably a stupid question so apologies in advance.

I am trying to format a number to look like a phone number with "-"'s
between the numbers etc e.g. 15554256987 should be formatted as
1-555-425-6987.

What have you tried?
What did you expect and what did your script do?

Are you sure all your numbers have exactly 11 digits?

Here's a 'brute force' method to insert dashes at the positions you
specified. You might want to turn it into a function ... and change
the positions (and quantity) of dashes to insert.

#v+
<?php
$number = '15554256987';

$number = substr($number, 0, 1) . '-' . substr($number, 1);
// now $number = '1-5554256987';

$number = substr($number, 0, 5) . '-' . substr($number, 5);
// now $number = '1-555-4256987';

$number = substr($number, 0, 9) . '-' . substr($number, 9);
// now $number = '1-555-425-6987';

#v-


Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
2
by: Dman | last post by:
Having trouble in Access XP. I want to display the customers name, city and phone number in a Combo Box but the but the formatting is lost – eg (123) 456-7890 is displayed as 1234567890. Any...
3
by: buck | last post by:
reports/ can we alternate formatting such as "no shading" and then "shading" for multiple records such as a phone directory in Access2000 reports
4
by: Brian Henry | last post by:
I have phone numbers like this in a data table 123-435-1234 1231231234 432.234.2321 they all have different formatting, what I want to do is get them all formatted like this (123) 123-1234
4
by: Earl | last post by:
I'm curious if there are others who have a better method of accepting/parsing phone numbers. I've used a couple of different techniques that are functional but I can't really say that I'm totally...
1
by: womblesjc | last post by:
I have a data bound Details View control in asp.net 2.0 that formats a phone number. The 'Default Mode' for the control is set to Edit. The phone number field is a template field and I can...
0
by: JeremyW | last post by:
I have a data bound Details View control in asp.net 2.0 that formats a phone number. The 'Default Mode' for the control is set to Edit. The phone number field is a template field and I can...
2
by: dcyale | last post by:
I have a report with the following paragraph: ="This BA presents a " & & " determination for the " & & " and associated habitat. We would appreciate you processing the biological opinion by " &...
4
by: Blue Streak | last post by:
Hello, Folks! Does anyone know of a website that lists the local phone number formats for each country? TIA...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.