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
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
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.