manix Posted June 22, 2011 Share Posted June 22, 2011 Hey guys wassup? I'm trying to import smileys in my page but the page seems to not load at all, can you tell me where I did wrong?. <?php $emote[1] = ""; $emoteimg[1] = "lol.gif"; $emote[2] = ":S"; $emoteimg[2] = "caring.gif"; $msg = " hahahahaha verry funny :S"; for ($x=1;$x=2;$x++){ for ($y=1;substr_count($msg, $emote[$x]);$y++){ $msg = substr_replace($msg, "<img src='emos/$emoteimg[$x]' />", strpos($msg, $emote[$x]), strlen($emote[$x])); }} echo $msg; ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 22, 2011 Share Posted June 22, 2011 You have an infinite loop for ($x=1;$x=2;$x++){ That loop will continue as long as you can continue to assign the value of '2' to the variable $x. You should have used something like: for ($x=1;$x<count($emote)+1;$x++){ But, the code is way more complex than it needs to be. Try this: //Create one array with key/value pairs $emote = array( "" => "lol.gif", ":S" => "caring.gif" ); //Convert each values to a full HTML image tag foreach($emote as &$value) { $value = "<img src='emos/{$value}' />"; } $msg = " hahahahaha verry funny :S"; //Replace ALL values with a single function $msg = str_replace(array_keys($emote), $emote, $msg); echo $msg; Output: <img src='emos/lol.gif' /> hahahahaha verry funny <img src='emos/caring.gif' /> By using a single array it is easy to see the relationship between the search and replacement terms Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 22, 2011 Share Posted June 22, 2011 just try something like: $emote[1] = ""; $emoteimg[1] = "lol.gif"; $emote[2] = ":S"; $emoteimg[2] = "caring.gif"; $msg = " hahahahaha very funny :S"; foreach($emote as $k=>$e){ $msg = str_replace($e,'<img src="emos/'.$emoteimg[$k].'" />',$msg); } echo $msg; Quote Link to comment Share on other sites More sharing options...
manix Posted June 22, 2011 Author Share Posted June 22, 2011 wow thank you. I still have much to learn I guess ^^ Quote Link to comment Share on other sites More sharing options...
Xtremer360 Posted June 23, 2011 Share Posted June 23, 2011 I looked at the topic subject as I couldn't stop laughing. Then the first line was Hey guys wassup? Anyone else find this post humorous. Quote Link to comment 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.