Jump to content

Something Other Than Explode()


dzhax
 Share

Recommended Posts

I am working on a script that can take in a string and print out each letter in the string in a random different color.I can make the printing out I just can't figure out how to pull each letter out, in order, and place it into an array. I could use the explode with " " as the seperater but I want each letter to be different not word.Any help is greatfull.$input = 'This is a test message';is my sample string value im working with.EDIT: Before I forget to ask, how do you determine how many array items there are? I was going to pass the array through a for...loop but it would need to know when to stop.

Edited by dzhax
Link to comment
Share on other sites

thanks ingolme.can someone help with the for...loop part i thought i knew it but i dont
You can use foreach instead of for. Or you could use count().
Link to comment
Share on other sites

Thanks everyone and for your help here's my script!!!

<?phperror_reporting(0);$input = 'W00t I am amazin! I just made this kickass script!!!';echo '<span style="font-size: 24px">' . test3($input) . '</span>';function test3($in){ 	$change = str_split($in);	foreach($change as $each){		$rnd_num = rand(1,10);		switch ($rnd_num){			case 1: 	$rndColor = 'blue'; 	break;			case 2: 	$rndColor = 'red'; 		break;			case 3: 	$rndColor = 'yellow'; 	break;			case 4: 	$rndColor = 'black'; 	break;			case 5: 	$rndColor = 'green'; 	break;			case 6: 	$rndColor = 'lime'; 	break;			case 7: 	$rndColor = 'grey'; 	break;			case 8: 	$rndColor = 'cyan'; 	break;			case 9: 	$rndColor = 'maroon'; 	break;			case 10: 	$rndColor = 'purple'; 	break;		}		$out = $out . '<span style="color: ' . $rndColor . '">' . $each . '</span>';	}	return($out);}?>

Of course if you know an easier / more efficient way to write this do tell :)Actually now that I think of it, would I be able to load file contents to make a complete file print out like this? Seems that multi-line documents do not work correctly when just loading the into a string.

Edited by dzhax
Link to comment
Share on other sites

Thanks everyone and for your help here's my script!!!
<?phperror_reporting(0);$input = 'W00t I am amazin! I just made this kickass script!!!';echo '<span style="font-size: 24px">' . test3($input) . '</span>';function test3($in){ 	$change = str_split($in);	foreach($change as $each){		$rnd_num = rand(1,10);		switch ($rnd_num){			case 1: 	$rndColor = 'blue'; 	break;			case 2: 	$rndColor = 'red'; 		break;			case 3: 	$rndColor = 'yellow'; 	break;			case 4: 	$rndColor = 'black'; 	break;			case 5: 	$rndColor = 'green'; 	break;			case 6: 	$rndColor = 'lime'; 	break;			case 7: 	$rndColor = 'grey'; 	break;			case 8: 	$rndColor = 'cyan'; 	break;			case 9: 	$rndColor = 'maroon'; 	break;			case 10: 	$rndColor = 'purple'; 	break;		}		$out = $out . '<span style="color: ' . $rndColor . '">' . $each . '</span>';	}	return($out);}?>

Of course if you know an easier / more efficient way to write this do tell :)

More efficient? Put the colors in an array, use .= insted of $out = $out ., use mt_rand() instead of rand()And you should initialize the $out variable before using it.
function test3($in){   $colors = array("blue", "red", "yellow", "black", "green", "lime", "grey", "cyan", "maroon", "purple");  $change = str_split($in);  $out = '';  foreach($change as $each){	$rnd_num = mt_rand(0,9);	$out .= '<span style="color: ' . $colors[$rnd_num] . '">' . $each . '</span>';  }  return($out);}

Edited by Ingolme
Link to comment
Share on other sites

Hmm... I added ur revisions and All that is showing is a ! that changes colors

<?phperror_reporting(0);$input = 'W00t I am amazin! I just made this kickass script!!!';echo '<span style="font-size: 24px">' . test3($input) . '</span>';function test3($in){  $colors = array("blue", "red", "yellow", "black", "green", "lime", "grey", "cyan", "maroon", "purple");  $change = str_split($in);  $out = '';  foreach($change as $each){	$rnd_num = mt_rand(0,9);	$out = '<span style="color: ' . $colors[$rnd_num] . '">' . $each . '</span>';  }  return($out);}?>

and no errors either i tried removing error_reportingEDIT:Opps silly me i saw the . before that = and thought it was a typo. What does .= do?

Edited by dzhax
Link to comment
Share on other sites

You've missed the "." in ".=". It's vital.Also, I'd change three additional things - return can be without any parenthesis, mt_rand() can be directly within the $colors call, and instead of plain 9, you could have count($colors), so that the mt_rand() gets adjusted as you add colors.i.e.

<?phperror_reporting(0);$input = 'W00t I am amazin! I just made this kickass script!!!';echo '<span style="font-size: 24px">' . test3($input) . '</span>';function test3($in){  $colors = array("blue", "red", "yellow", "black", "green", "lime", "grey", "cyan", "maroon", "purple");  $numColors = count($colors);  $change = str_split($in);  $out = '';  foreach($change as $each){	$out .= '<span style="color: ' . $colors[mt_rand(0, $numColors)] . '">' . $each . '</span>';  }  return $out;}?>

".=" is a syntax sugar.

$var1 operator= $var2

is equivalent of

$var1 = $var1 operator $var2

Where "operator" can be ".", or pretty much any aritmetic operator (+, -, *, /, &, |...).

Edited by boen_robot
Link to comment
Share on other sites

I know I'm basically changing the topic again, but is it possible to make text a gradient color? like vertical gradient?
Not easily, however the way Dragon Interactive does it is to have lots and lots of copies of the same phrase, each one color of the gradient, and then use CSS to only expose one of the copies on each row. Edited by Synook
Link to comment
Share on other sites

i just looked and I think i see what you mean. It seems their copyright is like that. So I ripped their css and tried it and it didnt work. Maybe reading the source is not enough tho.piecing it together I compiled this:

<style>/* In Rainbows */#stage h1.rainbows,#stage h2.rainbows {	display: block;	font: 36px/1.1 "Lucida Grande",verdana,sans-serif;	margin: -75px 0 0 -214px;	position: absolute;	top: 145px;	left: 50%;	width: 100%;	text-align: left;	xtext-transform: uppercase;	z-index: 3;	}#stage h2.rainbows { top: 135px; }#stage h3 {	color: #fff;	color: #ccc;	display: block;	font: 16px/1.1 "Lucida Grande",verdana,sans-serif;	margin: -75px 0 0 -214px;	margin: -75px 0 0 -210px;	position: absolute;	top: 180px;	left: 50%;	width: 100%;	text-align: left;	xtext-transform: uppercase;	z-index: 3;	}.rainbow {	background: transparent;	color: #aaa;	display: block;	position: relative;	height: 1px;	overflow: hidden;	z-index: 5;	}.rainbow span {	position: absolute;	display: block;	top: 0;	left: 0px;	width: 700px;	}.rainbows .highlight {	color: #000;	color: #fff;	display:block;	position: absolute;	top: -2px;	left: 0px;	z-index: 4;	width: 700px;	}.rainbows .shadow {	color: #000;	display:block;	position: absolute;	opacity: 0.5;	top: 0px;	left: 1px;	z-index: 3;	width: 700px;	}#footer .rainbows .highlight {	display: none;	top: 8px;	}#footer .rainbows .shadow {	opacity: 0.75;	left: 2px;	top: 14px;	}</style><body style="background-color: #2A2A2A"><p id="footer"><span class="rainbows">© 2009 Dragon Interactive. All Rights Reserved.</span></p>

Edited by dzhax
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...