我有一個PHP代碼,可以讀取文本文件並允許用戶完美地搜索單詞及其作品。使用PHP如何讀取多個TEXT文件並顯示文件名?
文件內容是阿拉伯語
當用戶進行搜索,系統會顯示與它存在的行號申請的字符串。
我現在想要的是讓系統讀取多個文本文件,當用戶請求一個單詞時,系統將顯示他找到用戶請求的文件的名稱。
這是可能的,如果我有100個文件,這個過程需要多長時間?
代碼:
<?php
$myFile = "arabic text.txt";
$myFileLink = fopen($myFile, 'r');
$line = 1;
if(isset($_POST["search"]))
{
$search =$_POST['name'];
while(!feof($myFileLink))
{
$myFileContents = fgets($myFileLink);
if(preg_match_all('/('.preg_quote($search,'/').')/i', $myFileContents, $matches))
{
foreach($matches[1] as $match)
{
echo "Found $match on Line $line";
}
}
++$line;
}
}
fclose($myFileLink);
//echo $myFileContents;
?>
<html>
<head>
</head>
<meta http-equiv="Content-Language" content="ar-sa">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<body>
<form action="index.php" method="post">
<p>enter your string <input type ="text" id = "idName" name="name" /></p>
<p><input type ="Submit" name ="search" value= "Search" /></p>
</form>
</body>
</html>
我會在執行函數建議grep命令,請參考此答案https://stackoverflow.com/a/17231293/6582942 – pravindot17
@ pravindot17首先我使用PHP和我需要閱讀的文字內容文件。 –
我知道這就是爲什麼我建議使用php的exec函數來運行linux的grep – pravindot17