Tidy Up Vanderbilt Phone Numbers With PHP

Vanderbilt has three telephone exchanges which consistently resolve to 5 digit extensions within campus. Numbers that begin with 322- have an extension that starts with 2-; 343- begins as 3-; and 936- begins as 6-. Recently I was asked to display phone numbers from a database where the numbers were entered inconsistently. To do this, I leaned on the PHP string token function — strtok.

For those who might need it or are learning how to use strtok, here are the gory details.

// Start phone number expansion
if ($row['telephone'] != "") {
$phone = $row['telephone'];
$phonestart = strtok($phone, "-");
switch ($phonestart) {
case '2':
$phone = '32' . $phone;
$phonestart = strtok($phone, "-");
break;
case '3':
$phone = '34' . $phone;
$phonestart = strtok($phone, "-");
break;
case '6':
$phone = '93' . $phone;
$phonestart = strtok($phone, "-");
break;
default:
break;
}
if ($phonestart != '615') {
$phone = '615-' . $phone;
}
echo $phone;
}
// End phone number expansion

0 comments ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment