2013-02-03 21 views
-3

我在我的服务器有一个数据diractory并没有像如何读取在PHP中一个txt文件

text1.txt 
abc.txt 
def.txt 

现在我需要阅读包含特定单词就像一个特定的行多个文本文件(EMP_NAME)从每个文件,并在我的PHP项目回声全行。 如何做到这一点任何帮助表示感谢提前。

+0

你事先知道文件名是什么,或者这是否改变? – Floris

+0

有很多方法可以读取一行,但我需要读取包含特定单词的行 – Shawon

+0

ya我知道文件名 – Shawon

回答

2

你的方向应该是这样的:

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"; 

?> 
+0

请发布代码示例 – Shawon

+0

您的代码只是找到我给出的字符串,但我需要打印满行的字符串 – Shawon

0

PHP代码:

$myFile = "testFile.txt"; 
$fh = fopen($myFile, 'r') 

如果你想通过文字

读字

检查this

This