如何在php中读取txt文件

如何在php中读取txt文件,php,Php,我有一个数据目录在我的服务器和有许多文本文件,如 text1.txt abc.txt def.txt 现在,我需要从每个文件中读取包含特定单词(emp_name)的特定行,并在我的php项目中回显完整的行。 如何做到这一点提前感谢您的帮助。PHP代码: $myFile = "testFile.txt"; $fh = fopen($myFile, 'r') 如果你想逐字读的话 退房 太你的方向应该是这样的: 1. firstly open the file , through fopen()

我有一个数据目录在我的服务器和有许多文本文件,如

text1.txt
abc.txt
def.txt
现在,我需要从每个文件中读取包含特定单词(emp_name)的特定行,并在我的php项目中回显完整的行。 如何做到这一点提前感谢您的帮助。

PHP代码:

$myFile = "testFile.txt";
$fh = fopen($myFile, 'r')
如果你想逐字读的话

退房


你的方向应该是这样的:

1. firstly open the file , through fopen()
2. then by file_get_contents() or fread() to get the content 
3. then search for the desired word in the file
<?php
$data = file_get_contents("input.txt"); //read the file
if(strpos($data,"str_to_find")==false){ //check if string exists in file
echo "str not found";
}
else 
echo "str found";

?>
编辑:
这样做:

1. firstly open the file , through fopen()
2. then by file_get_contents() or fread() to get the content 
3. then search for the desired word in the file
<?php
$data = file_get_contents("input.txt"); //read the file
if(strpos($data,"str_to_find")==false){ //check if string exists in file
echo "str not found";
}
else 
echo "str found";

?>


您是否提前知道文件名,或者这会改变吗?有很多方法可以读取一行,但是我需要读取一行包含特定单词的行。我知道文件名。在发布之前,您是否努力找到解决方案?请发布代码示例。您的代码只需找到我给出的字符串,但我需要打印包含该字符串的完整行