Solved

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

Posted on 2012-03-27
3
378 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.

747 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

13 Experts available now in Live!

Get 1:1 Help Now