Solved

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

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

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

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…
This article discusses four methods for overlaying images in a container on a web page
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.

738 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