megalos Posted September 25, 2011 Share Posted September 25, 2011 Hi I am trying to replace characters in a string with images so character 'a' will be replaced with a.jpg etc I have tried the following code but that doesn't result in the desired effect as the updated string gets it's characters replaced as the script works through it so the 'img' in the first updated string gets replaced with with images and it all ends up quite a mess. Is there a simpler way to do this? $content = "abc" ; $content = str_replace("a", "<img src=images/a.jpg >", $content); $content = str_replace("b", "<img src=images/b.jpg >", $content); $content = str_replace("c", "<img src=images/c.jpg >", $content); $content = str_replace("d", "<img src=images/d.jpg >", $content); $content = str_replace("e", "<img src=images/e.jpg >", $content); $content = str_replace("f", "<img src=images/f.jpg >", $content); $content = str_replace("g", "<img src=images/g.jpg >", $content); $content = str_replace("h", "<img src=images/h.jpg >", $content); $content = str_replace("i", "<img src=images/i.jpg >", $content); $content = str_replace("j", "<img src=images/j.jpg >", $content); $content = str_replace("k", "<img src=images/k.jpg >", $content); $content = str_replace("l", "<img src=images/l.jpg >", $content); $content = str_replace("m", "<img src=images/m.jpg >", $content); $content = str_replace("n", "<img src=images/n.jpg >", $content); $content = str_replace("o", "<img src=images/o.jpg >", $content); $content = str_replace("p", "<img src=images/p.jpg >", $content); $content = str_replace("q", "<img src=images/q.jpg >", $content); $content = str_replace("r", "<img src=images/r.jpg >", $content); $content = str_replace("s", "<img src=images/s.jpg >", $content); $content = str_replace("t", "<img src=images/t.jpg >", $content); $content = str_replace("u", "<img src=images/u.jpg >", $content); $content = str_replace("v", "<img src=images/v.jpg >", $content); $content = str_replace("w", "<img src=images/w.jpg >", $content); $content = str_replace("x", "<img src=images/x.jpg >", $content); $content = str_replace("y", "<img src=images/y.jpg >", $content); $content = str_replace("z", "<img src=images/z.jpg >", $content); echo $content ; Any help would be gratefully appreciated. Thanks Megalos (paul) Quote Link to comment https://forums.phpfreaks.com/topic/247841-replacing-string-characters-with-images/ Share on other sites More sharing options...
WebStyles Posted September 25, 2011 Share Posted September 25, 2011 maybe something like this: $content = str_split("abc"); foreach($content as $letter){ echo '<img src="images/'.$letter.'.jpg" >'; } Quote Link to comment https://forums.phpfreaks.com/topic/247841-replacing-string-characters-with-images/#findComment-1272644 Share on other sites More sharing options...
megalos Posted September 25, 2011 Author Share Posted September 25, 2011 Hi, That works are treat, thank you so very much. I wonder if you might be able to help me maintain any spaces in the output when the string is a phrase rather than a single word? Megalos(paul) Quote Link to comment https://forums.phpfreaks.com/topic/247841-replacing-string-characters-with-images/#findComment-1272646 Share on other sites More sharing options...
WebStyles Posted September 25, 2011 Share Posted September 25, 2011 $content = str_split("abc"); foreach($content as $letter){ echo $letter == '' ? ' ' : '<img src="images/'.$letter.'.jpg" >'; } Quote Link to comment https://forums.phpfreaks.com/topic/247841-replacing-string-characters-with-images/#findComment-1272648 Share on other sites More sharing options...
WebStyles Posted September 25, 2011 Share Posted September 25, 2011 lol.. I actually forgot to put the space between the ''. this one will work. Just replace ' ' with whatever you want in case you need smaller or bigger spaces. $content = str_split("abc"); foreach($content as $letter){ echo $letter == ' ' ? ' ' : '<img src="images/'.$letter.'.jpg" >'; } Quote Link to comment https://forums.phpfreaks.com/topic/247841-replacing-string-characters-with-images/#findComment-1272654 Share on other sites More sharing options...
megalos Posted September 25, 2011 Author Share Posted September 25, 2011 Hi, Thanks, I ended up with a modification to sit a blank image the same dimensions as the other character images for the spaces. Really appreciated your help. Sure you have guessed it's all a bit new to me but slowly getting there. The forum has been a great help. $content = str_split("abc"); foreach($content as $letter){ echo $letter == ' ' ? '<img src="images/blank.jpg" >' : '<img src="images/'.$letter.'.jpg" >'; } Megalos (Paul) Quote Link to comment https://forums.phpfreaks.com/topic/247841-replacing-string-characters-with-images/#findComment-1272655 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.