Monk3h Posted April 20, 2008 Share Posted April 20, 2008 Im using a basic PHP chat i made.. It simply adds the line and the sender into the database then brings it back in the format of $name Says: $message How would i make it so that when users type /me infront of there name it apears as $name $message? Aslo how would i make it so when people do an image command such as for the wink smiley. I know i would have to have the image in a file some where.. i just dont know how to get PHP to change the into the image. Anyhelp wil be much Aprechiated as Always. Link to comment https://forums.phpfreaks.com/topic/102003-solved-how-would-i-add-me-commands-and-smileys-into-my-chat-room/ Share on other sites More sharing options...
dptr1988 Posted April 20, 2008 Share Posted April 20, 2008 Use str_replace('/me', $name, $message_to_search); to replace '/me' with the name. Doing smilies can be a little more complicated. If you didn't want to write your own bbcode parser, I'm sure you could find some on google. You could also use a simple search and replace for bbcode Here is an example <?php $bbcode = array( ':-)' => '<img src="smily.png">', ':-(' => '<img src="sad.png">' ); $array_search = array_keys($bbcode); $array_replace = array_values($bbcode); $message = str_replace($array_search, $array_replace, $message); ?> Link to comment https://forums.phpfreaks.com/topic/102003-solved-how-would-i-add-me-commands-and-smileys-into-my-chat-room/#findComment-522024 Share on other sites More sharing options...
Monk3h Posted April 20, 2008 Author Share Posted April 20, 2008 [quote author=dptr1988 link=topic=193445.msg870069#msg870069 date=1208714550] Use str_replace('/me', $name, $message_to_search); to replace '/me' with the name. I dont quite understand this part. The exact code i use to display the users name and message in chat is this. print "<b>$chat[user]</b>: $chat[chat]<br>"; How would i aply this to the $chat[chat] part? $chat[chat] is the message and $chat[user] is the users name. Link to comment https://forums.phpfreaks.com/topic/102003-solved-how-would-i-add-me-commands-and-smileys-into-my-chat-room/#findComment-522031 Share on other sites More sharing options...
dptr1988 Posted April 20, 2008 Share Posted April 20, 2008 <?php $message = str_replace('/me', $chat['user'], $chat['chat']); print "<b>{$chat['user']}</b>: {$message}<br>"; ?> Link to comment https://forums.phpfreaks.com/topic/102003-solved-how-would-i-add-me-commands-and-smileys-into-my-chat-room/#findComment-522036 Share on other sites More sharing options...
Monk3h Posted April 20, 2008 Author Share Posted April 20, 2008 How would i make it so when /me is used in my chat the original $user is removed.. right now when i use /me im getting $name: $name $Message Without it i still get $name: $Message like before. But when /me is used i want the : removed.. and only 1 $name. eg, /me jumps = *$name Jumps Jumps = $name: Jumps Link to comment https://forums.phpfreaks.com/topic/102003-solved-how-would-i-add-me-commands-and-smileys-into-my-chat-room/#findComment-522042 Share on other sites More sharing options...
dptr1988 Posted April 20, 2008 Share Posted April 20, 2008 In this case you could compare the original message to the new one and see if there is a difference, if there is then print the message differently. Example: <?php $message = str_replace('/me', $chat['user'], $chat['chat']); if ($message == $chat['chat']) { // Only print the username if there weren't any '/me's in the message print "<b>{$chat['user']}</b>: "; } print "{$message}<br>"; ?> Or you could manually do the search/replace and the set a flag if you did find one. Link to comment https://forums.phpfreaks.com/topic/102003-solved-how-would-i-add-me-commands-and-smileys-into-my-chat-room/#findComment-522048 Share on other sites More sharing options...
Monk3h Posted April 20, 2008 Author Share Posted April 20, 2008 Thank you so much for your help.. How would i add the image string replace to this code? if you could add an example for 1 smiley i can do the rest. Link to comment https://forums.phpfreaks.com/topic/102003-solved-how-would-i-add-me-commands-and-smileys-into-my-chat-room/#findComment-522058 Share on other sites More sharing options...
dptr1988 Posted April 20, 2008 Share Posted April 20, 2008 This may not be the most efficiant way of doing this but is it simple. Here is the example I posted before, modifed to fit in with you existing code <?php // The values ( like 'smily' ) are converted to '<img src='smily'>' later on. // The keeps you from having to type the same HTML <img code over and over $bbcode = array( ':-)' => 'smily', ':-(' => 'sad' ); // Prepare the arrays to be used $array_search = array_keys($bbcode); $array_replace = array_values($bbcode); foreach($array_replace as $index=>$name) { $array_replace[$index] = "<img src='{$name}.png'>"; } // Look for and replace the '/me's $message = str_replace('/me', $chat['user'], $chat['chat']); if ($message == $chat['chat']) { // Only print the username if there weren't any '/me's in the message print "<b>{$chat['user']}</b>: "; } // Look for and replace all of the smilies with images $message = str_replace($array_search, $array_replace, $message); print "{$message}<br>"; ?> Link to comment https://forums.phpfreaks.com/topic/102003-solved-how-would-i-add-me-commands-and-smileys-into-my-chat-room/#findComment-522069 Share on other sites More sharing options...
Monk3h Posted April 20, 2008 Author Share Posted April 20, 2008 Thank you very much for all your help mate, Havnt had to debug once.. Perfect Syntax and this has to have been the best and fastest help i have had on this site so far. <3 Link to comment https://forums.phpfreaks.com/topic/102003-solved-how-would-i-add-me-commands-and-smileys-into-my-chat-room/#findComment-522082 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.