[snip]
> [stuff you may not need]
> This is a boiled down version of a longer function that counts string
> lengths to determine how many dashes might need to be added. Let's say
> you have the area code in the number, like 2108765432. Being a ten
digit
> number with a recognizable area code we can then add a portion to the
> function to add the two needed dashes, making the number more
readable.
> [/stuff]

I'd love to see that larger function, if you care to share.
[/snip]

Here is a little more of the larger function with comments (more
comments than code, which is never a Bad Thing [tm]). I am only showing
the handling for two basic types of telephone numbers with explanation
for additional verification which we would typically use, since we have
those resources available.

<?php
/*
** Dashing Through The TN's
** addTNDashes.func
**
*/
function addTNDashes($originalTN){
        /* 
        ** get the length of the number passed 
        ** you could also make sure that the string is numeric here
        ** and throw an appropriate error if not
        */
        $lengthTN = strlen($originalTN);
        /*
        ** in this example we are going to examine two basic types of
        ** phone numbers, the seven digit and the ten digit
        ** you can add other conditions for other standard formats of
        ** world wide TNs
        */
        if(7 == $lengthTN){
                /* 
                ** here we dash our seven digit number
                ** we could also validate the first three numbers as
                ** a valid NXX (exchange) if we had a list or DB of NXXs
                */
                $newTN = substr($originalTN, 0, 3) . "-" .
substr($originalTN, 3, 4);
        } elseif(10 == $lengthTN){
                /*
                ** here we dash our ten digit number
                ** we could also validate the first three numbers as
                ** a valid NPA (area code) if we had a list or DB of
NPAs
                */
                $newTN = substr($originalTN, 0, 3) . "-" .
substr($originalTN, 3, 3) . "-" . substr($originalTN, 6, 4);
        } elseif(7 < $lengthTN){
                /*
                ** the number is way too short to be a phone number
                ** let's throw an error and exit
                ** don't forget to set up some sort of error-logging for
this kind
                ** of thing for all of your errors
                */
                echo "The number you have supplied to the application
does not appear to be\n";
                echo "the correct format for any known telephone number.
Please correct this\n";
                echo "and restart application.\n";
                $appLog = fopen("application_error.log", "a");
                /* do error logging as you wish, dates, times, user ids,
etc */
                fclose($appLog);
                exit();
        }
        
        return $newTN;
}
?>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to