gpt4 book ai didi

javascript - 在字符串中回显 PHP

转载 作者:行者123 更新时间:2023-11-29 18:08:05 24 4
gpt4 key购买 nike

我正在运行一个简单的聊天应用程序,它由 process.php 文件提供支持,但聊天是在 chat.php 上进行的。

基本上人们可以搜索“主题”,它会将他们带到 domain.tld/chat.php?topic=topicname(主题名称是他们搜索的任何内容)

我需要我的 process.php 文件来回显

 <?php echo $_GET['topic']; ?>.txt 

而不是 chat.txt,这样每个主题都有一个唯一的文本文件(这样所有的聊天都不会链接)

这是我的 process.php 文件:

<?php

$function = $_POST['function'];

$log = array();

switch($function) {

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

case('update'):
$state = $_POST['state'];
if(file_exists('logs/chat.txt')){
$lines = file('logs/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("\n", "", $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) != "\n"){

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

$message = preg_replace('/#(\w+)/', ' <a href="@$1" target="_blank">#$1</a>', $message);


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

}

echo json_encode($log);

?>

这是我的 chat.js 文件

/* 
Created by: Kenrick Beckett

Name: Chat Engine
*/

var instanse = false;
var state;
var mes;
var file;

function Chat () {
this.update = updateChat;
this.send = sendChat;
this.getState = getStateOfChat;
}

//gets the state of the chat
function getStateOfChat(){
if(!instanse){
instanse = true;
$.ajax({
type: "POST",
url: "process.php",
data: {
'function': 'getState',
'file': file
},
dataType: "json",

success: function(data){
state = data.state;
instanse = false;
},
});
}
}

//Updates the chat
function updateChat(){
if(!instanse){
instanse = true;
$.ajax({
type: "POST",
url: "process.php",
data: {
'function': 'update',
'state': state,
'file': file
},
dataType: "json",
success: function(data){
if(data.text){
for (var i = 0; i < data.text.length; i++) {
$('#chat-area').append($("<p>"+ data.text[i] +"</p>"));
}
}
document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight;
instanse = false;
state = data.state;
},
});
}
else {
setTimeout(updateChat, 1500);
}
}

//send the message
function sendChat(message, nickname)
{
updateChat();
$.ajax({
type: "POST",
url: "process.php",
data: {
'function': 'send',
'message': message,
'nickname': nickname,
'file': file
},
dataType: "json",
success: function(data){
updateChat();
},
});
}

理论上,每当有人开始以不存在的主题聊天时,这应该在/logs/中创建一个唯一的 topicname.txt 文件。我只是无法在 process.php 中添加 topicname 来代替 chat.txt。到目前为止,我知道它确实会自己创建一个 chat.txt 文件,所以一旦我正确回应它,它应该会创建一个唯一的 .txt 文件。

此外,我知道与将消息存储在唯一的 .txt 文件中相比,数据库是更好的选择,但这就是我想要的方式。

这是一个示例,说明我如何尝试将它添加到我的 process.php(来自 process.php 的片段)

 case('getState'):
if(file_exists('logs/<?php echo $_GET['topic']; ?>.txt')){
$lines = file('logs/<?php echo $_GET['topic']; ?>.txt');
}

^ 这甚至可能不是正确的格式,因为我是 PHP 的新手并且犯了很多错误,而且它可能不知道 GET 是什么,因为它不是 chat.php 的一部分......这是一个单独的文件。

最佳答案

尝试 -

'logs/' . $filename . '.txt'

随心所欲。

更新

if (!empty($_GET['topic'])) {
$filename = $_GET['topic'];
} else {
$filename = 'something else';
}

if(file_exists('logs/' . $filename . '.txt')){ $lines = file('logs/' . $filename . '.txt') ....

它已经在 php 中了。所以不需要添加 <?php ?>echo .只需将它们连接起来即可。

关于javascript - 在字符串中回显 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29838799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com