douchen9855 2017-03-16 12:20
浏览 63

如何使用核心php发送表情符号与实时聊天并行?

we are using this script for integrate emoji but that is not working in other system. Current sytem can view the emojis but not able to view with other system.

<script>
     // ask user for name with popup prompt    
      var name = prompt("Enter your chat name:", "Guest");

      // default name is 'Guest'
      if (!name || name === ' ') {
     name = "Guest";  
 }
    // strip tags
    name = name.replace(/(<([^>]+)>)/ig,"");
    // display name on page
    $("#name-area").html("You are: <span>" + name + "</span>");
    // kick off chat
    var chat =  new Chat();
    $(function() {
       chat.getState(); 
         // watch textarea for key presses
         $("#sendie").keydown(function(event) {  
           var key = event.which;  
             //all keys including return.  
             if (key >= 33) {
               var maxLength = $(this).attr("maxlength");  
               var length = this.value.length;  

                 // don't allow new content if length is maxed out
                 if (length >= maxLength) {  
                   event.preventDefault();  
               }  
           }  
       });
         // watch textarea for release of key press
         $('#sendie').keyup(function(e) {   
          if (e.keyCode == 13) { 
            e.preventDefault();
            var text = $(this).val();
            var maxLength = $(this).attr("maxlength");  
            var length = text.length; 
            var str = '';
            str = $.trim($("#sendie").val());
                // send 
                if (length <= maxLength + 1) { 
                    $(this).val("");

                    $.ajax({
                        type: "POST",
                        url: "parsetext.php",
                        data: {str: addslashes(str)},
                        cache: false,
                        success: function(html) {

                           chat.send(text, name);  
                            $("#chat-area").append(name + html);

                        },
                        error: function(html) {
                                    //alert("Failed");
                                }
                            });


                } else {

                    $(this).val(text.substring(0, maxLength));

                }   
            }
        });
     });
function addslashes(str) {
    return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g,  '\\0');}
</script>
<body onload="setInterval('chat.update()', 1000)">

<div id="page-wrap">

    <h2>jQuery/PHP Chat</h2>

    <p id="name-area"></p>

    <div id="chat-wrap"><div id="chat-area"></div></div>

    <form id="send-message-area">
        <p>Your message: </p>
        <textarea id="sendie" maxlength = '100' ></textarea>
    </form>

</div>
</body>

When we have enter the any text that is viewed in another system but the emoji is not view with them.

<?php
$function = $_POST['function'];

$log = array();

switch($function) {

     case('getState'):
         if(file_exists('chat.txt')){
           $lines = file('chat.txt');
         }
         $log['state'] = count($lines); 
         break; 

     case('update'):
        $state = $_POST['state'];
        if(file_exists('chat.txt')){
           $lines = file('chat.txt');
         }
         $count =  count($lines);
         if($state == $count){
             $log['state'] = $state;
             $log['text'] = false;

             }
             else{
                 $text= array();
                 $log['state'] = $state + count($lines) - $state;
                 foreach ($lines as $line_num => $line)
                   {
                       if($line_num >= $state){
                     $text[] =  $line = str_replace("
", "", $line);
                       }

                    }
                 $log['text'] = $text; 
             }

         break;

     case('send'):
      $nickname = htmlentities(strip_tags($_POST['nickname']));
         $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
          $message = htmlentities(strip_tags($_POST['message']));
     if(($message) != "
"){

         if(preg_match($reg_exUrl, $message, $url)) {
            $message = preg_replace($reg_exUrl, '<a href="'.$url[0].'" target="_blank">'.$url[0].'</a>', $message);
            } 


         fwrite(fopen('chat.txt', 'a'), "<span>". $nickname . "</span>" . $message = str_replace("
", " ", $message) . "
"); 
     }
         break;

}

echo json_encode($log);
?>

i have a added the process.php file.

 <?php
  function parseString($string ) {
   $my_smilies = array(
    ':aln' => '<img src="images/alien1.png" alt="" />',
    ':thk' => '<img src="images/annoyed.png" alt="" />',
    ':ang' => '<img src="images/angel.png" alt="" />',
    ':slp<' => '<img src="images/zzz.png" alt="" />',
    ':blnk' => '<img src="images/blanco.png" alt="" />',
    ':zip' => '<img src="images/zip_it.png" alt="" />',
    ':bor' => '<img src="images/boring.png" alt="" />',
    ':brb' => '<img src="images/brb.png" alt="" />',
    ':bsy' => '<img src="images/busy.png" alt="" />',
    ':cell' => '<img src="images/cellphone.png" alt="" />',
    ':tp' => '<img src="images/clock.png" alt="" />',
    ':cool' => '<img src="images/cool.png" alt="" />',
    ':czy' => '<img src="images/crazy.png" alt="" />',
    ':cry' => '<img src="images/cry.png" alt="" />',
    ':dvl' => '<img src="images/devil.png" alt="" />',
    ':blush' => '<img src="images/blush.png" alt="" />',
    ':stop' => '<img src="images/dnd.png" alt="" />',
    ':flwr' => '<img src="images/flower.png" alt="" />',
    ':heart' => '<img src="images/heart.png" alt="" />',
    ':geek' => '<img src="images/geek.png" alt="" />',
    ':gift' => '<img src="images/gift.png" alt="" />',
    ':ill' => '<img src="images/ill.png" alt="" />',
    ':love' => '<img src="images/in_love.png" alt="" />',
    ':file' => '<img src="images/text_file.png" alt="" />',
    ':kiss' => '<img src="images/kissy.png" alt="" />',
    ':laugh' => '<img src="images/laugh.png" alt="" />',
    ':mail' => '<img src="images/mail.png" alt="" />',
    ':music' => '<img src="images/music2.png" alt="" />',
    ':whst' => '<img src="images/not_guilty.png" alt="" />',
    ':please' => '<img src="images/please.png" alt="" />',
    ':info' => '<img src="images/info.png" alt="" />',
    ':sad' => '<img src="images/sad.png" alt="" />',
    ':silly' => '<img src="images/silly.png" alt="" />',
    ':lol' => '<img src="images/oh.png" alt="" />',
    ':slps' => '<img src="images/speechless.png" alt="" />',
    ':srpd' => '<img src="images/surprised.png" alt="" />',
    ':tease' => '<img src="images/tease.png" alt="" />',
    ':wink' => '<img src="images/wink.png" alt="" />',
    ':grin' => '<img src="images/xd.png" alt="" />'
   );

   return str_replace( array_keys($my_smilies), array_values($my_smilies),         $string);
}

$str = stripslashes( $_POST["str"]);
echo '<strong>me: </strong> '.parseString($str).'<br>';
?>

i have a added the parsetext.php file.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?