RE: Formating a string to telephone format - (123) 456 - 7890
| From: | Ted Knudson | Date: | Tue, 27 Jun 2000 04:07:55 +0000 |
| Subject: | RE: Formating a string to telephone format - (123) 456 - 7890 | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-3278@lists.php.net to get a copy of this message | ||
The problem I had with phone numbers is that I had to have the number
formatted for Act!. It had to be 877-654-6547. This function takes care of
the problem. Some people like to input their numbers like 800.654.6541,
1(800)654-9874, 1*654*654*9876, etc. Act! does not like it.
The first thing it does is takes out any leading 1 and all formatting.
You can try it at http://shops.automatedcabinets.com/phone.php3
Ted Knudson
http://www.automatedcabinets.com
<--Start Code-->
<HTML>
<HEAD>
<TITLE>Phone Number Function</TITLE>
</HEAD>
<BODY BGCOLOR=#FFFFFF TEXT=#000000>
<?php
function PhoneNumber(&$phonevar)
{
$phonevar = ereg_replace("[^[:digit:]]", "", $phonevar);
if(substr($phonevar,0,1)== 1 and strlen($phonevar) >= 11)
{
$phonevar = ereg_replace('^1(.{3})(.{3})(.{4})', '\\1-\\2-\\3', $phonevar);
}
elseif(substr($phonevar,0,1)<> 1 and strlen($phonevar) >= 10)
{
$phonevar = ereg_replace('^(.{3})(.{3})(.{4})', '\\1-\\2-\\3', $phonevar);
}
elseif(substr($phonevar,0,1)== 1 and strlen($phonevar) <= 10)
{
$phonevar = "Not a valid phone number";
}
elseif(strlen($phonevar) <= 9)
{
$phonevar = "Not a valid phone number";
}
return($phonevar);
}
?>
<FORM ACTION=phone.php3 METHOD=POST>
<INPUT TYPE=TEXT NAME="phone" SIZE=12>
<INPUT TYPE=SUBMIT NAME=Calculate VALUE=Calculate>
<?php
print "<BR>".PhoneNumber($phone)."<BR>";
?>
</body>
</HTML>
<--End Code -->
> -----Original Message-----
> From: Sean Mathews [mailto:smathews@dceg.com]
> Sent: Monday, June 26, 2000 9:19 AM
> To: php-general@lists.php.net
> Subject: Formating a string to telephone format - (123) 456 - 7890
>
>
> Heya Guys
>
> This is surely very simple, but Im new. I need to create a function to
> format a variable length string to a telephone format. Thus 1234567890
> becomes (123) 456 - 7890, and 1234567 becomes 123 - 4567 etc etc.
> I havent
> been able to find any procedures to copy portions of strings. Please drop
> me a hint.
>
>