[英]Echo PHP inside of string
我正在運行一個簡單的聊天應用程序,它由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
。 只需將它們串聯即可。
您已經在php標簽中了。無需添加額外的php標簽
case('getState'):
if(file_exists("logs/".$_GET['topic'].".txt")){
$lines = file("logs/".$_GET['topic'].".txt");
}
或嘗試一下
case('getState'):
if(isset($_GET['topic']){
$filename = "logs/".$_GET['topic'].".txt";
if(file_exists($filename)){
$lines = file($filename);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.