Solved

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

Posted on 2011-09-30
7
319 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
  • 3
  • 3
7 Comments
 
LVL 108

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 108

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 108

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now