Lars! Posted February 14, 2010 Share Posted February 14, 2010 function BText($string) { $string = str_replace("a","<img src='images/a.png'/>",$string); $string = str_replace("b","<img src='images/b.png'/>",$string); $string = str_replace("c","<img src='images/c.png'/>",$string); $string = str_replace("d","<img src='images/d.png'/>",$string); $string = str_replace("e","<img src='images/e.png'/>",$string); $string = str_replace("f","<img src='images/f.png'/>",$string); $string = str_replace("g","<img src='images/g.png'/>",$string); $string = str_replace("h","<img src='images/h.png'/>",$string); $string = str_replace("i","<img src='images/i.png'/>",$string); $string = str_replace("j","<img src='images/j.png'/>",$string); $string = str_replace("k","<img src='images/k.png'/>",$string); $string = str_replace("l","<img src='images/l.png'/>",$string); $string = str_replace("m","<img src='images/m.png'/>",$string); $string = str_replace("n","<img src='images/n.png'/>",$string); $string = str_replace("o","<img src='images/o.png'/>",$string); $string = str_replace("p","<img src='images/p.png'/>",$string); $string = str_replace("q","<img src='images/q.png'/>",$string); $string = str_replace("r","<img src='images/r.png'/>",$string); $string = str_replace("s","<img src='images/s.png'/>",$string); $string = str_replace("t","<img src='images/t.png'/>",$string); $string = str_replace("u","<img src='images/u.png'/>",$string); $string = str_replace("v","<img src='images/v.png'/>",$string); $string = str_replace("w","<img src='images/w.png'/>",$string); $string = str_replace("x","<img src='images/x.png'/>",$string); $string = str_replace("y","<img src='images/y.png'/>",$string); $string = str_replace("z","<img src='images/z.png'/>",$string); $string = nl2br($string); return $string; } I'm currently using this to replace every letter with an image. However sometimes it doesn't work correctly. Basically it would replace the word "hia" with "<img src='images/h.png'/><img src='images/i.png'/><<img src='images/i.png'/>mg src='images/<<img src='images/h.png'/>mg src='images/i.png'/>.png'/>" When it gets to the letter i it seems to replace it with <img src='images/i.png'/> Please help. ~Thanks Lars! Quote Link to comment https://forums.phpfreaks.com/topic/192061-str_replace-help/ Share on other sites More sharing options...
sader Posted February 14, 2010 Share Posted February 14, 2010 u shoudl do it in one call like so $search = array('a','b','c'); $replace = array("<img src='a.png'>","<img src='b.png'>","img src='c.png'"); $str = str_replace($search, $replace, $string); also u may want to look at function str_ireplace which is case insensitive Quote Link to comment https://forums.phpfreaks.com/topic/192061-str_replace-help/#findComment-1012272 Share on other sites More sharing options...
Lars! Posted February 14, 2010 Author Share Posted February 14, 2010 It does the same thing :S Quote Link to comment https://forums.phpfreaks.com/topic/192061-str_replace-help/#findComment-1012283 Share on other sites More sharing options...
jl5501 Posted February 14, 2010 Share Posted February 14, 2010 dont forget, str_replace works on the entire source string each time so you need to wrap your letters in something that will not be in the replaced string Quote Link to comment https://forums.phpfreaks.com/topic/192061-str_replace-help/#findComment-1012288 Share on other sites More sharing options...
Lars! Posted February 14, 2010 Author Share Posted February 14, 2010 Quote Link to comment https://forums.phpfreaks.com/topic/192061-str_replace-help/#findComment-1012291 Share on other sites More sharing options...
jl5501 Posted February 14, 2010 Share Posted February 14, 2010 if you can arrange it, so your original letters are like '{a}' instead of just 'a' then you can get this to work with your way the i in images would itself get replaced, etc. Quote Link to comment https://forums.phpfreaks.com/topic/192061-str_replace-help/#findComment-1012292 Share on other sites More sharing options...
Lars! Posted February 14, 2010 Author Share Posted February 14, 2010 im lost lol Quote Link to comment https://forums.phpfreaks.com/topic/192061-str_replace-help/#findComment-1012293 Share on other sites More sharing options...
sader Posted February 14, 2010 Share Posted February 14, 2010 damn what about using regexp try this: $result = preg_replace('/([a-z])/i', "<img src='images/\\1.png'>", $string); in case u have different letter pictures for lower and upper chars use this $result = preg_replace('/([a-zA-Z])/', "<img src='images/\\1.png'>", $string); Quote Link to comment https://forums.phpfreaks.com/topic/192061-str_replace-help/#findComment-1012295 Share on other sites More sharing options...
jl5501 Posted February 14, 2010 Share Posted February 14, 2010 ok a question here, which may help resolve this. Do the letters you wish to replace form part of words, or do they have space either side of them? Quote Link to comment https://forums.phpfreaks.com/topic/192061-str_replace-help/#findComment-1012303 Share on other sites More sharing options...
Andy-H Posted February 14, 2010 Share Posted February 14, 2010 Came up with a solution: function BText($str) { $new = ''; for($i = 0; $i < strlen($str); $i++) { if (ctype_alpha($str[$i])) { $new .= '<img src="images/' . strtolower($str[$i]) . '" />'; } else { $new .= $str[$i]; } } return $new; } $str = 'hia, pal!'; $str = BText($str); echo $str; OUTPUT SOURCE <img src="images/h" /><img src="images/i" /><imgsrc="images/a" />, <img src="images/p" /><imgsrc="images/a" /><img src="images/l" />! Quote Link to comment https://forums.phpfreaks.com/topic/192061-str_replace-help/#findComment-1012337 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.