Solved

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

Posted on 2011-09-30
7
331 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Append letter to end of string 4 36
File submission to SQL Server 1 38
WordPress syntax 2 25
highcharts wont fix inside a bootstrap modal window 6 27
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 is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 count occurrences of each item in an array.

911 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

20 Experts available now in Live!

Get 1:1 Help Now