Jump to content

Exploding function (almost literally...)


ensellitis

Recommended Posts

Ok, what did I do wrong here...

 

<?php 
function letters2image($the_start) {
$splitting = explode(" ", $the_start);
$le = "";
$si=0; 
$word_start = "<div>";
$word_end = "</div>";
$finish = "";

foreach ( $splitting as $key_value ) {
	$s_len=strlen($key_value);

	for ($si=0;$si<$s_len;$si++) {
		$c=$key_value[$si]; 
		if($c == " ") { $c = "space"; }
		$le .= "<img src='letters/".$c.".png' />"; 
	}

	$finish .= $word_start.$le.$word_end;
}
return $finish;
}
?>

 

The words are supposed to be wrapped in DIVs, and all the letters are supposed to be replaced by an image.  However, it spitting out something like this:

 

letters2image("The quick brown fox jumps over the lazy dog")

 

returns (assume the letters are images)

 

<div>The </div>

<div>The quick </div>

<div>The quick brown </div>

<div>The quick brown fox </div>

<div>The quick brown fox jumps </div>

<div>The quick brown fox jumps over </div>

<div>The quick brown fox jumps over the </div>

<div>The quick brown fox jumps over the lazy </div>

<div>The quick brown fox jumps over the lazy dog</div>

 

I know I'm missing something, something simple probably

Link to comment
https://forums.phpfreaks.com/topic/197206-exploding-function-almost-literally/
Share on other sites

Simple fix here, you need to reset the $le variable to blank/empty after/before you have processed each word, that's why it is coming out the way it is.

 

foreach ( $splitting as $key_value ) {

  $le = ''; // reset the letter string
  $s_len=strlen($key_value);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.