0

I have a PHP code that read text file and allow the user to make a search on a word and its work perfectly.

the files content is in arabic

Where the user make a search and the system will display the requested string with the line number where it exist.

What i want now is to make the system read multiple text files and when the user request a word the system will display the name of files where he found the user request.

is this possible and how long this process will take if i have 100 files ?

code:

<?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>
Mahi Parmar
  • 515
  • 3
  • 10
  • 31
Rany Fahed
  • 39
  • 3
  • 11

2 Answers2

0

So you have to put your currently working code into a function, with the function returning the content you want.

function readFile($fileName)
{
  // ...your code
  return $yourMessage;
}

$fileNames = array("file1.txt", "file2.txt");
$result = array();
foreach($fileNames as $name)
{
  $message = readFile($name);
  $result[$name] = $message;
}

You can now iterate over the $result and print it in your own desired way.

Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105
  • i want the system to read the TEXT files that exist in a specified directory i do not want to hardoced the name of files because each day more files will be added. – Rany Fahed Dec 27 '17 at 12:52
0

To get all files into one array you can use scandir('/mydir/').

$files = scandir('/mydir/here/');
foreach($files as $file) {
    if (strpos($file, '.txt')) {
        //dosomething
    }
}
halojoy
  • 225
  • 2
  • 7