Emoticons

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Emoticons

Post by oldtimer »

I am trying to make it so when I make a post on my site a : ) will display as a smiliy face. (note the extra spaces so it shows here :) )

I have been reading up on preg replace but just dont quite know how to do it. I Currently use preg replace to get rid of extra white spaces. I do not want to just search for one symbol to replace. I want to go though a Database of icons to show. I also do not want to permanantly replace them like doing it before storing into database. Just when it is shown.

So if I have a $message how do you go though and preg replace and then have it print out?


At this moment I have looked at over 50 posts here and none of them helped me. I have checked out php.net as well. I even have looked at all my php books.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

assuming you have a list of smilies, by name, and the image is name.gif

(you might need to adjust to use keys)

here's what i do (that i know works)

Code: Select all

foreach($smileys as $smiley){
    $postcode=preg_replace("|\[$smiley]|i", '<img src="sitepics/smilies/$smiley.gif">', $postcode); # smileys
  }
but the code i made has [] around the smiley name
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

is your list in a database? I just want a : ) to show a smiley and was setting it up in a database to show the correct one. My database I put in the symbols and what graphic goes with it.

How do you get your $smileys?
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

i have a function to read the directory they are in and grab the names, that way i can add to the directory, rename in the directory, or remove from the directory and everything gets updated automatically
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

I see that but if someone just typed in the normal : ) for smile it has to link to something.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

the links are reacted from the list of the smilies
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

Okay. I see what you are saying. they click on a smilie and it does it. I was thinking of if you did not click on a smilie but just typed in the normal symbols.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

heh, i don't have my code help page set up to insert smilies on clicking or for them to even display int he post. just when used, although the help page calls the list and goes through the array to tell the user what is there.
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

I finally found out how to do this. Now I ended up not doing it from a Database as it does not like the ) or the ?.

Code: Select all

<?php
 $patterns[0] = "/:D/";         $replacements[0] = "<img src="$path/images/icon_biggrin.gif">";
 $patterns[1] = "/:shock:/";   $replacements[1] = "<img src="$path/images/icon_eek.gif">";
 $patterns[2] = "/:x/";         $replacements[2] = "<img src="$path/images/icon_mad.gif">";
 $patterns[3] = "/:evil:/";     $replacements[3] = "<img src="$path/images/icon_evil.gif">";
 $patterns[4] = "/:!:/";       $replacements[4] = "<img src="$path/images/icon_exclaim.gif">";
 $patterns[5] = "/:\)/";        $replacements[5] = "<img src="$path/images/icon_smile.gif">";
 $patterns[6] = "/:\?/";        $replacements[6] = "<img src="$path/images/icon_confused.gif">";
 $patterns[7] = "/:P/";         $replacements[7] = "<img src="$path/images/icon_razz.gif">";
 $patterns[8] = "/:twisted:/";  $replacements[8] = "<img src="$path/images/icon_twisted.gif">";
 $patterns[9] = "/:\?:/";      $replacements[9] = "<img src="$path/images/icon_question.gif">";
 $patterns[10] = "/:\(/";       $replacements[10] = "<img src="$path/images/icon_frown.gif">";
 $patterns[11] = "/:C/";         $replacements[11] = "<img src="$path/images/icon_cool.gif">";
 $patterns[12] = "/:oops:/";   $replacements[12] = "<img src="$path/images/icon_redface.gif">";
 $patterns[13] = "/:roll:/";   $replacements[13] = "<img src="$path/images/icon_rolleyes.gif">";
 $patterns[14] = "/:idea:/";   $replacements[14] = "<img src="$path/images/icon_idea.gif">";
 $patterns[15] = "/:o/";        $replacements[15] = "<img src="$path/images/icon_surprised.gif">";
 $patterns[16] = "/lol/";    $replacements[16] = "<img src="$path/images/icon_lol.gif">";
 $patterns[17] = "/:cry:/";    $replacements[17] = "<img src="$path/images/icon_cry.gif">";
 $patterns[18] = "/:wink:/";  $replacements[18] = "<img src="$path/images/icon_wink.gif">";

$toshow = preg_replace($patterns, $replacements, $text);
echo $toshow;

?>
I put this in a file and just call it up right before I display my text.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

hmmm i should be aware of that
Post Reply