jakebur01 Posted June 17, 2011 Share Posted June 17, 2011 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); Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 17, 2011 Share Posted June 17, 2011 Try this: <?php $string = "(555)555-5555"; echo preg_replace("/[^0-9]/","", $string); ?> Quote Link to comment Share on other sites More sharing options...
jakebur01 Posted June 17, 2011 Author Share Posted June 17, 2011 What about converting the periods and forward slashes to dashes? Would I do something like this? $fax=str_replace('/','-',$fax); $fax=str_replace('.','-',$fax); $fax = preg_replace("/[^0-9]/","", $fax) Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 17, 2011 Share Posted June 17, 2011 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"); ?> Quote Link to comment Share on other sites More sharing options...
jakebur01 Posted June 17, 2011 Author Share Posted June 17, 2011 I really need to keep the dashes and replace forward slashes and periods with dashes. Jake Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 17, 2011 Share Posted June 17, 2011 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.