|
Posted by drako on 10/10/40 11:51
Hi,
I'm having a major headache with what I expected to be a simple
problem, which is to take a string of text, split it down to an array
of individual characters (including spaces), then cycle through the
array and create HTML which assigns specific images to the characters -
i.e. 'a' will be replaced by '<img src="a.jpg"'>, etc....
However, if I pass a string to the following function (such as "John
James Smith"), it goes so far, then stops converting the letters to
images. I suspect that it has something to do with the foreach loop
encountering spaces ?
Not exactly sure what could be going on here, and how to move forwards.
Any suggestions ?
Thanks
Neil.
code below
------------------------------------
function convert_text_to_images($text) {
$split = preg_split('//', strtolower($text), -1, PREG_SPLIT_NO_EMPTY);
$convert = array("a" => "a.jpg", "b" => "b.jpg", "c" => "c.jpg", "d" =>
"d.jpg", "e" => "e.jpg", "f" => "f.jpg", "g" => "g.jpg", "h" =>
"h.jpg", "i" => "i.jpg", "j" => "j.jpg", "k" => "k.jpg", "l" =>
"l.jpg", "m" => "m.jpg", "n" => "n.jpg", "o" => "o.jpg", "p" =>
"p.jpg", "q" => "q.jpg", "r" => "r.jpg", "s" => "s.jpg", "t" =>
"t.jpg", "u" => "u.jpg", "v" => "v.jpg", "w" => "w.jpg", "x" =>
"x.jpg", "y" => "y.jpg", "z" => "z.jpg", "0" => "0.jpg", "1" =>
"1.jpg", "2" => "2.jpg", "3" => "3.jpg", "4" => "4.jpg", "5" =>
"5.jpg", "6" => "6.jpg", "7" => "7.jpg", "8" => "8.jpg", "9" =>
"9.jpg");
$imgsrc_string="";
foreach($split as $key=>$value){
if(array_key_exists($key, $convert)){$imgsrc_string .= '<img
src="'.$convert[$value].'" alt="'.$value.'">'; }
elseif ($value == ' ') {$imgsrc_string .= '<img src="space.jpg"
alt=" ">';}
else {$imgsrc_string .= $value;}
}
return($imgsrc_string);
}
Navigation:
[Reply to this message]
|