Solved

Need to break up a phone number and insert Twilio Play Verbs for playback in PHP

Posted on 2011-09-30
7
373 Views
Last Modified: 2012-05-12
Hello,
My application receives a phone number in this format:
8001234567

My goal is to have the application play back the phone number using a series of mp3 files (this is a twilio application if that helps). For area codes I have the possible mp3 files saved as:
800.mp3 for example (for the above number) and for each digit in the remaining number I have a single mp3 file from 0 to 9 to read out the rest of the digits. *I'm finding the area code sounds more natural when read as an area code instead of 3 separate numbers like the rest of the phone number.

So I'm trying to break up the number and insert the necessary code to play back the mp3's.

If above was the number, I need it to turn out like:

 
<Play>800.mp3</Play>
<Play>1.mp3</Play>
<Play>2.mp3</Play>
<Play>3.mp3</Play>
<Play>4.mp3</Play>
<Play>5.mp3</Play>
<Play>6.mp3</Play>
<Play>7.mp3</Play>

Open in new window


Essentially:
first 3 numbers are grouped together
then each number encased by the <Play> verb as shown.

Any idea how to do this?
0
Comment
Question by:tjyoung
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36894771
You can use the PHP substr() and split() functions to do this.

Here is how to filter / normalize a telephone number.
<?php // RAY_phone_numbers.php - USING USA PHONE NUMBERS
error_reporting(E_ALL);

// A FUNCTION TO VALIDATE A PHONE NUMBER AND RETURN A NORMALIZED STRING
// MAN PAGE: http://discuss.fogcreek.com/joelonsoftware3/default.asp?cmd=show&ixPost=102667&ixReplies=15
function strtophone($phone, $format=FALSE, $letters=FALSE, $dlm='-')
{
    if ($letters)
    {
        // TRANSLATE INPUT LIKE 1-800-BIG-DOGS
        $phone = strtoupper($phone);
        if (preg_match('/[A-Z]/', $phone))
        {
            $phone = str_replace('A', '2', $phone);
            $phone = str_replace('B', '2', $phone);
            $phone = str_replace('C', '2', $phone);

            $phone = str_replace('D', '3', $phone);
            $phone = str_replace('E', '3', $phone);
            $phone = str_replace('F', '3', $phone);

            $phone = str_replace('G', '4', $phone);
            $phone = str_replace('H', '4', $phone);
            $phone = str_replace('I', '4', $phone);

            $phone = str_replace('J', '5', $phone);
            $phone = str_replace('K', '5', $phone);
            $phone = str_replace('L', '5', $phone);

            $phone = str_replace('M', '6', $phone);
            $phone = str_replace('N', '6', $phone);
            $phone = str_replace('O', '6', $phone);

            $phone = str_replace('P', '7', $phone);
            $phone = str_replace('Q', '7', $phone);
            $phone = str_replace('R', '7', $phone);
            $phone = str_replace('S', '7', $phone);

            $phone = str_replace('T', '8', $phone);
            $phone = str_replace('U', '8', $phone);
            $phone = str_replace('V', '8', $phone);

            $phone = str_replace('W', '9', $phone);
            $phone = str_replace('X', '9', $phone);
            $phone = str_replace('Y', '9', $phone);
            $phone = str_replace('Z', '9', $phone);
        }
    }

    // DISCARD NON-NUMERIC CHARACTERS
    $phone = preg_replace('/[^0-9]/', NULL, $phone);

    // DISCARD A LEADING '1' FROM NUMBERS ENTERED LIKE 1-800-555-1212
    if (substr($phone,0,1) == '1') $phone = substr($phone,1);

    // IF LESS THAN TEN DIGITS, IT IS INVALID
    if (strlen($phone) < 10) return FALSE;

    // IF IT STARTS WITH '0' OR '1' IT IS INVALID, SECOND DIGIT CANNOT BE '9' (YET)
    if (substr($phone,0,1) == '0') return FALSE;
    if (substr($phone,0,1) == '1') return FALSE;
    if (substr($phone,1,1) == '9') return FALSE;

    // ISOLATE THE COMPONENTS OF THE PHONE NUMBER
    $ac = substr($phone,0,3); // AREA
    $ex = substr($phone,3,3); // EXCHANGE
    $nm = substr($phone,6,4); // NUMBER
    $xt = substr($phone,10);  // EXTENSION

    // ADD OTHER TESTS HERE AS MAY BE NEEDED - THESE ARE FOR LOCAL APPS
    if ($ac == '900') return FALSE;
    if ($ac == '976') return FALSE;
    if ($ex == '555') return FALSE;

    // IF NOT FORMATTED
    if (!$format) return $phone;

    // STANDARDIZE THE PRINTABLE FORMAT OF THE PHONE NUMBER LIKE 212-555-1212-1234
    $formatted_phone = $ac . $dlm . $ex . $dlm . $nm;
    if ($xt != '') $formatted_phone .= $dlm . $xt;
    return $formatted_phone;
}



// DEMONSTRATION OF THE FUNCTION IN ACTION.
if (!empty($_GET["phone"]))
{

    // VALIDATE PHONE USING FUNCTION ABOVE
    if (!$phone = strtophone($_GET["phone"]))
    {
        // FUNCTION RETURNS FALSE IF PHONE NUMBER IS UNUSABLE
        echo "BOGUS: {$_GET["phone"]} ";
    }
    else
    {
        // SHOW THE FORMATTED PHONE
        echo "VALID: {$_GET["phone"]} == $phone";
    }
}


// END PHP, PUT UP THE FORM TO TEST PHONE NUMBERS
?>
<form>
ENTER A PHONE NUMBER:
<input name="phone" /><br/>
<input type="submit" />
</form>
TRY SOME OF THESE (COPY AND PASTE):
<br/>1-800-5551212
<br/>202-537-7560
<br/>202 537 7560
<br/>1-202-537-7560
<br/>(202) 537-7560
<br/>1.202.537.7560
<br/>123456789
<br/>703-356-5300 x2048
<br/>(212) 555-1212
<br/>1 + (212) 555-1212
<br/>1 (292) 226-7000

Open in new window

0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36894772
I would use str_split() to break the string into an array.  http://www.w3schools.com/php/func_string_str_split.asp
A more comprehensive list of string functions can be found at   http://www.w3schools.com/php/php_ref_string.asp

Frankly, I would break the area code into separate digits as well.  Area codes that end in 00 can be said as  N hundred.  Other area codes such as 212 probably should be said as two one two instead of two hundred and twelve.

You can make a special case for the 00 area codes simply by looking at index 1 and 2 to see if they are both 0.

Do you need more help or is this sufficient?
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36894779
Or you could do what Ray said.  I'm pretty sure he tested that code and it works.

Regardless, I think you should read about the string functions at w3schools if you haven't already.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 36894795
See if this does the job for you...
http://www.laprbass.com/RAY_temp_tjyoung.php
<?php // RAY_temp_tjyoung.php
error_reporting(E_ALL);
echo "<pre>";

// NEATLY PACKAGED AS A FUNCTION
function twilio_phone($number, $tag="Play")
{
    $ac = substr($number, 0, 3);
    $nb = substr($number, 3);
    $xx = str_split($nb);

    $out = '<' . $tag . '>' . $ac . '</' . $tag . '>' . PHP_EOL;

    foreach ($xx as $x)
    {
        $out .= '<' . $tag . '>' . $x . '</' . $tag . '>' . PHP_EOL;
    }

    return $out;
}


// TEST THE FUNCTION
echo htmlentities(twilio_phone('8001234567'));

Open in new window

0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 36894806
beautiful/clean. Works like a charm.
thanks as always.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36894821
Ray, that looks correct in that it meets the spec.  I question if the author really has an mp3 that says "two hundred and twelve."  However, it's not clear he needs it.  Perhaps all #'s are 800's.  (Still, I suspect an 888 number could get in there.)
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36894841
@tjyoung, thanks for the points and for your kind words.  @hmccurdy, excellent point.  Worth looking out for unusual variants.  Twilio has its own unique ways of saying and listening.  For anyone who is interested, Twilio is a really unique and elegantly simple platform for building telephone apps.  Jeff Lawson runs the company. He was one of the software visionaries behind StubHub.
http://twilio.com

Cheers, ~Ray
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question