0

i have PHP code that open TXT file and read it then allow the user to make a search on any word, if it exist it will display it with the line number.

i need the code to search for the user input and display the word with the line number as a first step.

i will appreciate any help.

the problem is that when i run the code it doesn't display anything.

code:

<?php


$myFile = "test.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;
 }

}  

fclose($myFileLink);


?>

<html>
    <head>

    </head>
    <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>
Rany Fahed
  • 39
  • 3
  • 11
  • i tested your code, it works as expected. Can you provide your test case (txt file contents and the text you are searching) – RDev Dec 26 '17 at 07:52
  • @RDev the text content is in arabic and i am searching for an existing word, text size is 15KB – Rany Fahed Dec 26 '17 at 07:55
  • with english it works good. I think that you need to handle the unicode correctly for the arabic alpabeth, look at this answer [here](https://stackoverflow.com/questions/41958144/arabic-characters-using-preg-match-function/41964028) – RDev Dec 26 '17 at 08:00
  • I've also tested your code and it works well for me (in English) – Simon R Dec 26 '17 at 08:01
  • @RDev I replaced /i with /u as it says in the answer but still nothing hapen. how to deal with arabic or correct unicode ? – Rany Fahed Dec 26 '17 at 11:06

0 Answers0