如何将表情添加到聊天室?

时间:2016-03-20 11:40:59

标签: php html session

我想在我简单的聊天室中添加表情符号。我找到了功能" Smilify"但我不知道如何将它实现到我的聊天室。你能帮我解决这个问题吗?

功能Smilify:

function Smilify(&$subject)
{
    $smilies = array(
        ':|'  => 'mellow',
        ':-|' => 'mellow',
        ':-o' => 'ohmy',
        ':-O' => 'ohmy',
        ':o'  => 'ohmy',
        ':O'  => 'ohmy',
        ';)'  => 'wink',
        ';-)' => 'wink',
        ':p'  => 'tongue',
        ':-p' => 'tongue',
        ':P'  => 'tongue',
        ':-P' => 'tongue',
        ':D'  => 'biggrin',
        ':-D' => 'biggrin',
        '8)'  => 'cool',
        '8-)' => 'cool',
        ':)'  => 'smile',
        ':-)' => 'smile',
        ':('  => 'sad',
        ':-(' => 'sad',
    );

    $sizes = array(
        'biggrin' => 18,
        'cool' => 20,
        'haha' => 20,
        'mellow' => 20,
        'ohmy' => 20,
        'sad' => 20,
        'smile' => 18,
        'tongue' => 20,
        'wink' => 20,
    );

    $replace = array();
    foreach ($smilies as $smiley => $imgName)
    {
        $size = $sizes[$imgName];
        array_push($replace, '<img src="imgs/'.$imgName.'.gif" alt="'.$smiley.'" width="'.$size.'" height="'.$size.'" />');
    }
    $subject = str_replace(array_keys($smilies), $replace, $subject);
}

我简单的聊天室:

<?php
require_once './DBconnect.php';

if ((isset($_SESSION['user']) and strlen($_SESSION['user']) > 0)) {

} else {

    header("Location: index.php?page=signin");
    $_SESSION['unlogged'] = "<div style='position:absolute;left:29.5%;top:14%;width:41%;' class='alert alert-danger'>
             <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
             <strong>Chyba!</strong> Pro p&#345;&#237;stup na tuto str&#225;nku mus&#237;te b&#253;t p&#345;ihl&#225;&#353;en/a!
             </div>";
}

$message = mysqli_real_escape_string($conn, $_POST['message']);

$sql_name = "SELECT username FROM users WHERE username='$_SESSION[user]'";
$result_name = mysqli_query($conn, $sql_name);
while ($row_name = mysqli_fetch_array($result_name)) {

    $name = $row_name ['username'];
}

if (($message) != "") {

    mysqli_query($conn, "INSERT INTO `chat`(`username`, `message`,`time`) VALUES ('$name','$message',Now())");
}
?>

<!-- Content -->
<!--<meta http-equiv="refresh" content="5" >-->
<div id="content-content">

    <h3><center>Glob&#225;ln&#237;</center></h3>                      
    <textarea id="chat-window" readonly=""><?php $vyber = mysqli_query($conn, "SELECT username, message, time FROM `chat` ORDER BY `time`");
        while ($radek = mysqli_fetch_assoc($vyber)) {
            echo htmlspecialchars($radek['time'] . " " . $radek['username'] . ": " . $radek['message'] . "\r\n");
        }
        ?></textarea>

    <textarea id="online-users" readonly="">Online</textarea>

    <form method="post" name="chatWriteform" onsubmit="return validate();">
        <textarea id="chat-write" name="message" maxlength = "300" placeholder="Va&#154;e zpr&#225;va..."></textarea>
    </form>
</div>

1 个答案:

答案 0 :(得分:1)

在不更改Smilify功能的情况下,我会将您的代码更改为:

while ($radek = mysqli_fetch_assoc($vyber)) {
  $message = htmlspecialchars($radek['message']);
  Smilify($message);
  echo htmlspecialchars($radek['time']) . " " . htmlspecialchars($radek['username']) . ": " . $message . "\r\n");
}