?
Solved

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

Posted on 2012-03-27
3
Medium Priority
?
386 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 14

Assisted Solution

by:EMB01
EMB01 earned 1000 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 35

Accepted Solution

by:
gr8gonzo earned 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

777 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