Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

86 users online



Smileys - How to make those nifty things work

Smileys - How to make those nifty things work

Currently viewing this thread: 1 (0 members and 1 guests)


Jeb

Jeb

Status: Offline!

Smileys - How to make those nifty things work

[b][i]Please note that this guide is simply how [u]I[/u] do smileys. Other people may do it differently - that's fine. This is my preferred method because it's fast, simple, and easy to understand.[/i][/b]

I've searched these forums and haven't found any guides on making smileys. I've seen BBCode, shoutcode, etc, but no smileys. So, here we go.

[b]This Guide Assumes The Following:[/b]
[LIST=a]
[*]You know how to use associative arrays
[/LIST]

---------

In the words of php_brian:

[b][i]If I got a dime every time I've seen this type of topic posted.......You know the rest :cool:[/i][/b]

How true! This has to be one of the most common questions asked...second only to those evil register_global questions.

But it's pointless giving out free code everytime this question is asked. That teaches you absolutely nothing (unless you're one of the few rare people who reads, learns from and tinkers with sample code ^_^. To those people, I salute you.)

[i]Give a man a fish and you'll feed him for a day. Teach a man to fish and you'll feed him for his lifetime.[/i]

First of all, let's decide what sort of smiley's we're looking for. We'll look for all the traditional ones, like :), :(, :D, :o, :p, etc.

Let's put all these into an associative array, each one mapped to it's own image filename.

[php]
<?php

$smileys = array(
':)' => 'smile.gif',
':(' => 'sigh.gif',
':D' => 'biggrin.gif',
':o' => 'omigosh.gif',
':p' => 'tongue.gif',
';)' => 'wink.gif');

?>
[/php]

Right, that's taken care of. Each smiley has been mapped to a specific image filename.

So, we now need some text to work with. Let's just use a simple string, and assign it to the $text variable:

[php]
<?php

$smileys = array(
':)' => 'smile.gif',
':(' => 'sigh.gif',
':D' => 'biggrin.gif',
':o' => 'omigosh.gif',
':p' => 'tongue.gif',
';)' => 'wink.gif');

$text = "Hi, this is my smiley string :). Isn't it cool? :D I thought so too. :p";

?>
[/php]

Easy, simple, no tricks there.

With those two things out of the way, we now have to replace them. We can accomplish this by using the [b][url=http://www.php.net/str-replace]str_replace()[/url][/b] function.

The str_replace() function prototype looks like this:

[b]mixed str_replace ( mixed search, mixed replace, mixed subject )[/b]

In simple terms: The value of [i]subject[/i] is searched for the value of [i]search[/i], and if it is found, it will replace it with the value of [i]replace[/i].

What we need to do is go through each smiley in the $smileys array, replacing it with it's image name and an HTML <img> tag. Easy! All you'll need is the following, and you'll be laughing:

[LIST=1]
[*]A foreach() loop.
[*]The str_replace() function.
[/LIST=1]

[php]
<?php

$smileys = array(
':)' => 'smile.gif',
':(' => 'sigh.gif',
':D' => 'biggrin.gif',
':o' => 'omigosh.gif',
':p' => 'tongue.gif',
';)' => 'wink.gif');

$text = "Hi, this is my smiley string :). Isn't it cool? :D I thought so too. :p";
foreach($smileys as $smiley=>$image) {
$text = str_replace($smiley, '<img src="images/'.$image.'">', $text);
}

echo $text;
?>
[/php]

The loop is straight-forward, you should all know what that does. Separate each $smileys key=>value pair into the variables $smiley and $image.

We then use str_replace() to search for $smiley in $text, and if found, replace it with the $image variable placed into the HTML <img> tag.

Piece of cake, no?.

And that's it! That's a barebones guide to adding smileys to just about any string you can think of. All you need to do is replace $text with whatever variable holds your string.

If you've got any questions or queries, please post them in this thread. If you've got any suggestions, please offer them.

Don't feel limited by hardcoding your smileys. You can also pull the array data from a database if you want, or even from a flat file. It's all up to your imagination: remember, there's more than one way to skin a cat.

RESOURCES:
[url=http://www.php.net/str-replace]str_replace() function[/url]
[url=http://au3.php.net/manual/en/control-structures.foreach.php]foreach() loop[/url]

___________________

Adam Goossens -- PHP is my mother tounge.

Linux: ( kernel.org | winehq ) -- f33l the p0w3r.

Nobody replying to your questions? Getting flamed? Getting told to RTFM? Ask your questions the right way.

Last edited by Jeb, March 15th, 2003 08:20 PM (Edited 1 times)

inigoesdr

inigoesdr

Status: Offline!

Very Nice! Wink

stewis

stewis

Neverside Newbie
Status: Offline!

very handy piece of info i am sure some one will find it handy...

keep up the good work Smile

___________________

Current status: Taken
Somethings Coming: 07/07/07
**stewis lubs Sarah Smile**

Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0171 seconds.