Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Txt文件中搜索和查找。然后使用找到的变量_Php_Search_Text_Find - Fatal编程技术网

Php 如何在Txt文件中搜索和查找。然后使用找到的变量

Php 如何在Txt文件中搜索和查找。然后使用找到的变量,php,search,text,find,Php,Search,Text,Find,好的,这是我正在做的另一个项目 它是一个聊天客户端。并将其用于员工 我希望服务器上有一个staff.txt 我希望php文件能够做到这一点 执行php 如果在staff.txt中找到提交的用户名,则 用户名更改为[Staff]“此处的用户名” 我找到了搜索结果 我似乎无法保留提交的用户名,而只是向其中添加工作人员 我现在正在添加我的源代码 <?php // Parameters (Leave this Alone) $Message = $_GET["message"]; $Usern

好的,这是我正在做的另一个项目

它是一个聊天客户端。并将其用于员工

我希望服务器上有一个staff.txt

我希望php文件能够做到这一点

执行php

如果在staff.txt中找到提交的用户名,则

用户名更改为[Staff]“此处的用户名”

我找到了搜索结果

我似乎无法保留提交的用户名,而只是向其中添加工作人员

我现在正在添加我的源代码

<?php
// Parameters (Leave this Alone)
 $Message = $_GET["message"];
 $Username = htmlspecialchars($_GET["username"]);
 $time = ($_GET["time"]);
 // User Banning
 $data = file_get_contents('Banned.txt');
 if(strpos($data, $Username) !== FALSE)
{
   die();
 }
 else
{

 // File Writing (Leave this Alone) 
 $File = "Chat.txt"; 
 $Handle = fopen($File, "a"); 
 fwrite($Handle, $Username);
 fwrite($Handle, ": ");
 fwrite($Handle, $Message);
  fwrite($Handle, " -:-:- ");
  fwrite($Handle, $time);
  fwrite($Handle, "\r\n");
  print "Message Sent"; 
  fclose($Handle); 
 }
?> 
在运行php的php文件中,这似乎可以正常工作


有没有办法将该列表保存在单独的文件中。txt文件或.php不重要。

您可以像banlist一样操作:

<?php
 // Parameters (Leave this Alone)
 $Message = $_GET["message"];
 $Username = htmlspecialchars($_GET["username"]);
 $time = ($_GET["time"]);

// check staff
 $data = file_get_contents('staff.txt');
 if(strpos($data, $Username) !== FALSE)
     $Username = '[STAFF]' . $Username;

// User Banning
 $data = file_get_contents('Banned.txt');
 if(strpos($data, $Username) !== FALSE)
{
   die();
 }
 else
{

 // File Writing (Leave this Alone) 
 $File = "Chat.txt"; 
 $Handle = fopen($File, "a"); 
 fwrite($Handle, $Username);
 fwrite($Handle, ": ");
 fwrite($Handle, $Message);
  fwrite($Handle, " -:-:- ");
  fwrite($Handle, $time);
  fwrite($Handle, "\r\n");
  print "Message Sent"; 
  fclose($Handle); 
 }
?> 


看起来这会很慢。你为什么不使用数据库呢?我有一个调节客户端,信不信由你,它不是一个真正的聊天系统。它的用途就像一篇博客文章。所以它不需要存在于数据库中,如果你死在if中,你也不需要else。这是一个隐含的else。也许不是真正的主题,但罗恩,你对大写字母的使用非常出色。你的意思是做
“[Staff]”$用户名?PHP检查我的编辑,这种方式可能更容易。对不起,我想我真的不明白。也许可以试试:
$Username='!分隔器![工作人员]”$用户名然后它会给我!分隔器![工作人员]!分隔器!用户名:message然后查看插入密码的代码!分隔器!别指望我会有更多的评论。我现在出去了。
<?php
 // Parameters (Leave this Alone)
 $Message = $_GET["message"];
 $Username = htmlspecialchars($_GET["username"]);
 $time = ($_GET["time"]);

// check staff
 $data = file_get_contents('staff.txt');
 if(strpos($data, $Username) !== FALSE)
     $Username = '[STAFF]' . $Username;

// User Banning
 $data = file_get_contents('Banned.txt');
 if(strpos($data, $Username) !== FALSE)
{
   die();
 }
 else
{

 // File Writing (Leave this Alone) 
 $File = "Chat.txt"; 
 $Handle = fopen($File, "a"); 
 fwrite($Handle, $Username);
 fwrite($Handle, ": ");
 fwrite($Handle, $Message);
  fwrite($Handle, " -:-:- ");
  fwrite($Handle, $time);
  fwrite($Handle, "\r\n");
  print "Message Sent"; 
  fclose($Handle); 
 }
?>