Best way to assign each number to a wav file

Hi,
hard to explain in my title but what I'm doing is I"m breaking out a phone number and for each of the digits in the phone number I'm assigning a wav file ie: number 9 would equal 9.wav

So when the app reads the number it will read it out using the wav files.

The code I use to break up the number by separating each with a comma is below. Not sure its the best way to go about it, but how would I get the numbers assigned to the appropriate wav files?

Hope it makes some sense... the number comes over as 2221234567 as an example. So my code inserts commas so the text to speech will read it back slow. But I want to assign each number a wave file instead.
 
$numberslow = wordwrap($humanNumber,1,",",TRUE);

Open in new window

LVL 1
tjyoungAsked:
Who is Participating?
 
Ray PaseurCommented:
I think you can record your own voice on Twilio and use the recorded pronunciations.

Here is how to create the play tags.
<?php // RAY_temp_tjyoung.php
error_reporting(E_ALL);


$num = '1234567890';
$arr = str_split($num);
$new = '<play>' . implode('.wav</play><play>', $arr) . '.wav</play>';
echo htmlentities($new);

Open in new window

0
 
ukerandiCommented:
if numbers come like this  2221234567  how do you need to get output?
0
 
Ray PaseurCommented:
First step would be to normalize the numbers.  You can do that with this function.  You may want to set the delimiter to NULL.  You will get a 10-digit string.

Next, you would use str_split() to convert the string to an array of numbers
http://us.php.net/manual/en/function.str-split.php

Finally you would use foreach() to iterate over the array to select each number one at at time.

In case you are using Twilio for your app, they can already say numbers pretty well, and you will not need to use your own .wav files.  Just a thought, ~Ray
<?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"], TRUE))
    {
        // 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
tjyoungAuthor Commented:
Need to get the output to look something like:
for:
1234567890
<PLAY>1.wav</PLAY><PLAY>2.wav</PLAY><PLAY>3.wav</PLAY> and so on...
0
 
tjyoungAuthor Commented:
Ray:
I've got the numbers coming in consistently as a 10 digit number (I strip the +1 from Twilio). Just couldn't figure out how to get the PLAY inserted between each. Taking a look at your link now
0
 
tjyoungAuthor Commented:
Sorry, also: twilio has a problem with the number 4 (pronounces 'fir') Told them about 6 months ago, don't think they're going to change anytime soon. And would be much more fluid if I can somehow pull it off with the same voice that is being used.
0
 
tjyoungAuthor Commented:
Perfect. Thanks!
0
 
tjyoungAuthor Commented:
Sorry, I just noticed: when I run it I seem to get this instead of the <play>
&lt;Play&gt;1.wav&lt;/Play&gt;&lt;

How do I keep it from converting to url encoding (I think thats what it is)
0
 
tjyoungAuthor Commented:
sorry, just figured it out. duh. Thanks again
0
 
Ray PaseurCommented:
I just used htmlentities() so I could read the output ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.