ensellitis Posted April 1, 2010 Share Posted April 1, 2010 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 More sharing options...
the182guy Posted April 1, 2010 Share Posted April 1, 2010 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); Link to comment https://forums.phpfreaks.com/topic/197206-exploding-function-almost-literally/#findComment-1035123 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.