繁体   English   中英

在字符串中回显PHP

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM