基本上我有一个聊天室,我想知道如何实现这个目标;基本上这个聊天室的工作方式是用户在场中输入消息,然后将其发布到html文档上,然后将html文档回显到聊天室并每2秒刷新一次我的问题是如何让它如此那个:wow
而不是仅仅:wow
出来会被贴出来作为图片吗?这样,聊天室就更能吸引人们的照片和情感。有没有上一篇文章或其他内容,我无法找到符合我要求的任何内容。
聊天室很简单,这里是显示房间的代码;
<script>
$(document).ready(function(){
setInterval(function(){
$("#chathistory1").load('broadcast/uploads/<? echo $row['username'];?>/<? echo $row['username'];?>.html')
}, 2000);
});
</script>
<div id="chathistory1"></div>
表格
<form action="chat.php" target="my_iframe" method="post" enctype="multipart/form-data" name="form1" id="form1" OnSubmit="setTimeout('clearForm()', 200); return true">
<div class="input-group">
<input name="name" type="hidden" id="name" size="16" maxlength="32" value="<?php echo $usr['username']; ?>" />
<input type="hidden" name="n" id="n" value="<?php echo $room ?>" />
<input id="btn-input" type="text" name="message" autocomplete="off" id="message" class="form-control input-sm" placeholder="Type your message here..." />
<span class="input-group-btn">
<button class="btn btn-primary btn-sm" id="btn-chat" onclick="testSubmit()">
Send</button></form>
答案 0 :(得分:0)
如果您正在阅读PHP中的内容,则可以使用preg_replace()
:
$re = "/\:(wow)/";
$str = "OMG :wow have you seen this?";
$rep = "<img src='wowimg.gif'>";
$newstr = preg_replace($re, $rep, $str);
echo $newstr; //will output "OMG <img src='wowimg.gif'> have you seen this?"
或者,在JavaScript中使用replace()
method:
var str = "OMG :wow have you seen this?";
var res = str.replace(":wow", "<img src='wowimg.gif'>");
console.log(res); //will output "OMG <img src='wowimg.gif'> have you seen this?"
在你的代码中,我想后者更容易:
$(document).ready(function(){
setInterval(function(){
$.get('broadcast/uploads/<? echo $row['username'];?>/<? echo $row['username'];?>.html', function(data) {
var newdata = data.replace(/\:wow/g, "replacement!");
$("#chathistory1").html(newdata);
});
}, 2000);
});