Solved

Trying to explode a 4 digit number and place <Play> statements in

Posted on 2012-03-27
3
381 Views
Last Modified: 2012-03-27
HI,
I'm generating a random 4 digit code and trying to get some 'Play' statements in between the letters. I'm thinking the code below explains my attempt better. Its wrong, just not sure why its failing.


//get random 4 digit approval id
$approval_id = rand(1001,9998);

//lets assume the random number was 2345
//here I'm not sure if you can explode a string without some kind of separator?

$breakit = explode("", $newapproval_id);

//trying to get new variable to be: <Play>audio/2.wav</Play><Play>audio/3.wav</Play>... and so on

$approval = "<Play>audio/<?php echo $breakit[0]?>.wav</Play><Play>audio/<?php echo $breakit[1]?>.wav</Play><Play>audio/<?php echo $breakit[2]?>.wav</Play><Play>audio/<?php echo $breakit[3]?>.wav</Play>";
?>

Open in new window

0
Comment
Question by:tjyoung
3 Comments
 
LVL 14

Assisted Solution

by:EMB01
EMB01 earned 250 total points
ID: 37772835
You could use str_split http://php.net/manual/en/function.str-split.php

Here's an example:

$breakit = str_split($newapproval_id);

//trying to get new variable to be: <Play>audio/2.wav</Play><Play>audio/3.wav</Play>... and so on

foreach ($breakit as $a)
{

    $approval .= '<Play>audio/'.$a.'.wav</Play>';

}

echo $approval;
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 250 total points
ID: 37772851
<?php

//get random 4 digit approval id
$approval_id = rand(1001,9998);

// Generate string
$approval = "";
for($i = 0; $i < strlen($approval_id); $i++)
{
  $approval .= "<Play>audio/".substr($approval_id,$i,1) . ".wav</Play>";
}

?>
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 37772927
Thank you. I used the second one but the first one looked equally promising.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Split wordpress loop 35 56
Why will this INSERT statement not insert the date? 2 21
Redirect 301 from one address  to another 5 26
Test if unique ID is in log file 5 22
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

828 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