array question(smilies)

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
User avatar
HaVoC
Forum Commoner
Posts: 83
Joined: Sat Feb 07, 2004 7:20 am
Location: Smiths Falls, CA

array question(smilies)

Post by HaVoC »

I have made a simple chatroom, but I wish to put in smilies...

Would this work?

Code: Select all

<?php
$message[':)'] = '<img src="smile.jpg">';
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

have you tried it?
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

Code: Select all

$newpostinfo = $_POST['message'];
//start smiley replace
$newpostinfo = str_replace(':D','<img src=images/smiles/icon_biggrin.gif>',$newpostinfo);
$newpostinfo = str_replace(':)','<img src=images/smiles/icon_smile.gif>',$newpostinfo);
$newpostinfo = str_replace(':(','<img src=images/smiles/icon_sad.gif>',$newpostinfo);
$newpostinfo = str_replace(':o','<img src=images/smiles/icon_surprised.gif>',$newpostinfo);
$newpostinfo = str_replace('8O','<img src=images/smiles/icon_eek.gif>',$newpostinfo);
$newpostinfo = str_replace(':?','<img src=images/smiles/icon_confused.gif>',$newpostinfo);
$newpostinfo = str_replace('8)','<img src=images/smiles/icon_cool.gif>',$newpostinfo);
$newpostinfo = str_replace(':lol:','<img src=images/smiles/icon_lol.gif>',$newpostinfo);
$newpostinfo = str_replace(':x','<img src=images/smiles/icon_mad.gif>',$newpostinfo);
$newpostinfo = str_replace(':P','<img src=images/smiles/icon_razz.gif>',$newpostinfo);
//end smiley replace
thats what i use in my forum
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Code: Select all

<?php
$message[':)'] = '<img src="smile.jpg">';
?>
That code won't replace smiley faces, but it will set-up the start of a replacement array which could be used with str_replace.
Why have you put slashes before the "quote marks" ? You don't need them when using single quotes around your string.
User avatar
HaVoC
Forum Commoner
Posts: 83
Joined: Sat Feb 07, 2004 7:20 am
Location: Smiths Falls, CA

Post by HaVoC »

feyd wrote:have you tried it?
Yeah I did and as 'launchcode' said it doesn't work.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use dull's suggestion.. overall..
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Or another way (just saves loads of str_replace calls).

Code: Select all

<?php
$smilies = array(
    ':D',
    ':)'
);
$smiliesreplace = array(
    '<img src="images/smiles/icon_biggrin.gif">',
    '<img src="images/smiles/icon_smile.gif">'
);
$_POST['message'] = str_replace($smilies, $smiliesreplace, $_POST['message']);
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Yeah, I would suggest the array route too - str_replace being made to work on arrays was one of the best moves the PHP developers made IMHO :)
Post Reply