Jump to content

Emoticons yo


manix

Recommended Posts

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;
?>

Link to comment
https://forums.phpfreaks.com/topic/240124-emoticons-yo/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/240124-emoticons-yo/#findComment-1233396
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.