Jump to content

[SOLVED] How would i add /me Commands and Smileys into my Chat room?


Monk3h

Recommended Posts

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.

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);
?>

[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.

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

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.

 

 

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>";


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.