I have a PHP code that reads multiple txt files and search for user request for a word if the word exist the code will display the the requested word with the line number and the file name.
the code work perfect but the problem is in the line number because the system take all the txt file and combine it in one file and continue counting the line number .
what i want is to make the system start from the first line in each file
example :
word محلي exist in 7 files on the same line 13 the system display this :
- the word محلّي exist on line [13] in 4-4-2017.txt
- the word محلّي exist on line [77] in arabic text.txt
- the word محلّي exist on line [175] in XXXX10-4-2017.txt
- the word محلّي exist on line [240] in XXXX5-4-2017.txt
- the word محلّي exist on line [302] in XXXX6-4-2017.txt
- the word محلّي exist on line [367] in XXXX7-4-2017.txt
- the word محلّي exist on line [431] in XXXX-08-04-2017.txt
yet this word exist on all these file on line 13
code:
<?php
$line = 1;
if(isset($_POST["search"]))
{
$search =$_POST['name'];
foreach(glob($_SERVER['DOCUMENT_ROOT']."/readfiletest/*.txt") as $txts)
{
$myFileLink = fopen($txts, 'r');
while(!feof($myFileLink))
{
$myFileContents = fgets($myFileLink);
if( preg_match_all('/('.preg_quote($search,'/').')/i', $myFileContents, $matches))
{
foreach($matches[1] as $match)
{
echo "the word $match exist on line [$line] in ";
}
echo basename ($txts) . "<br>".PHP_EOL;
//++$line;
}
++$line;
}
fclose($myFileLink);
}
}
?>
<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>