[SOLVED] Checking for smilies in a text area and changing th

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
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Checking for smilies in a text area and changing them...

Post by jonas »

I'm working on the forums area of my site now and I want to make a bunch of smilies for users to use.

The text area that they write their message in (like this one) should be checked for say a : + ) and replace it with a :) but only if it is on it's on. Not if its in a word or something.

Anyways, I need some help on the best way to do this and to make it automated as well.

Say the admins can put a new smilie in the backend with a command and it would automatically update posts and new posts.

Thanks
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

str_replace()
http://us4.php.net/str_replace

Code: Select all

$string = str_replace(':)', '<img src="http://forums.devnetwork.net/images/smiles/icon_smile.gif">', $string);
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

I'm inserting smilies as bbcode img's. Avoids problems like those in the above post with the smiley showing up in a php code section. No need to turn smilies on or off either.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

To avoid the problem above you just need to use a better regex to find out of its really a smilly:

(humm:-) = NO
(humm :-) ) = OK
words[space][smile][space]words = OK
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

To avoid the problem above you just need to use a better regex to find out of its really a smilly
Even with perfect regular expressions there's still human error. I'm also not saying my way is any better either. Every choice has it's pro's and con's. One big drawback of my method is increased data.

:-) = 3 bytes
[ img][ /img] = 11+ bytes

It avoids second guessing the user but at a cost.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Or you go for something that people are less likely to make mistakes in printing like [smile] [laugh] [sulk] [cry], as long as you have a key to show users how to use them, or some javascript to add them then its all good.
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Post by jonas »

Ok that's cool but say I wanted to make a backend for it and have it so it automatically updates without me having to insert a new line of code,.......
I guess I could insert a table in the DB that holds the smilies and their URLs/activate code and in the query while, do the check with variables.
Then if I add one into the database via a backend it would work automatically.

I guess you can ignore this, I seem to have talked through the solution myself. :) haha
Post Reply