Jump to content

formatting fax number


jakebur01

Recommended Posts

Hello,

 

I have  database of fax number and I need to copy these numbers to a separate table on a daily basis.  The format the customer service employees have been using are: (555)555-5555, 555/555-5555, 555.555.5555, 555/555-5555 FAX, (fax)555/555-5555, etc.

 

I would like to correct and strip the fax number before having them transferred into the database that the actually fax server will be using.

 

Jake

$fax=odbc_result($rs,"PHONE_2");

$fax = strtolower($fax);
$fax=str_replace('/','-',$fax);
$fax=str_replace('.','-',$fax);
$find=array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "%", "!", "@", "#", "$", "^", "&", "*","~", "+", ":", "=", "?", "'", "<", '\\', '(', ')', '[', ']', '{', '}');
str_replace($find, '', $fax);
$fax=trim($fax);

 

Link to comment
Share on other sites

Here, run this code, I moved the regexp to a function, the all print out "5555555555"

<?php
function get_numbers($string){
return preg_replace("/[^0-9]/","", $string);
}
echo get_numbers("(555)555-5555");
echo "<br>";
echo get_numbers("555/555-5555");
echo "<br>";
echo get_numbers("555.555.5555");
echo "<br>";
echo get_numbers("555/555-5555 FAX");
echo "<br>";
echo get_numbers("(fax)555/555-5555");
?>

Link to comment
Share on other sites

If you want to format the numbers in a consistent manner, you should FIRST use The Little Guy's code to strip out ALL characters that are not numbers. THEN you can apply the formatting. But there is a decision to be made on how to format it if there are not 10 characters. If the input is not 10 digits you can either leave it unformatted, return false (and not allow it), or determine some other way to format it.

 

function getFormattedFax($inputFax)
{
    //Strip out all non-numeric characters
    $faxNum = preg_replace("/[^\d]/", "", $inputFax);
    //If10 characters, apply formatting
    $formattedFax = (strlen($faxNum)==10) ? preg_replace('/(\d{3})(\d{3})(\d{4})/', '$1-$2-$3', $faxNum) : $faxNum; 
    return $formattedFax;
}

echo  getFormattedFax('123-456-7890');   //123-456-7890
echo  getFormattedFax('(123) 456-7890'); //123-456-7890
echo  getFormattedFax('123.456.7890');   //123-456-7890
echo  getFormattedFax('123-4567');       //1234567
echo  getFormattedFax('123456');         //123456
echo  getFormattedFax('1234567890 FAX'); //123-456-7890
echo  getFormattedFax('FAX: 1234567890');//123-456-7890
echo  getFormattedFax('FAX 1234567');    //1234567

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.