Hello, task: Write a function that displays a list of files in a given directory that contain the search word. Directory and search word are set as function parameters. I wrote everything correctly, as I understand it in the preg_match () function, which should look for a match for a regular expression and return 1 if there is a match, it returns zero to me everywhere, although there is a file with such text, I canβt understand why the regular expression can compiled incorrectly, although it was checked on the site http://regexr.ru/ The following is the full php code:
<html> <head> <meta charset="utf-8"/> <title>ΠΠ°Π΄Π°Π½ΠΈΠ΅ 5</title> <style> </style> </head> <body> <h3>5. ΠΠ°ΠΏΠΈΡΠ°ΡΡ ΡΡΠ½ΠΊΡΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ Π²ΡΠ²ΠΎΠ΄ΠΈΡ ΡΠΏΠΈΡΠΎΠΊ ΡΠ°ΠΉΠ»ΠΎΠ² Π² Π·Π°Π΄Π°Π½Π½ΠΎΠΉ Π΄ΠΈΡΠ΅ΠΊΡΠΎΡΠΈΠΈ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠΎΠ΄Π΅ΡΠΆΠ°Ρ ΠΈΡΠΊΠΎΠΌΠΎΠ΅ ΡΠ»ΠΎΠ²ΠΎ. ΠΠΈΡΠ΅ΠΊΡΠΎΡΠΈΡ ΠΈ ΠΈΡΠΊΠΎΠΌΠΎΠ΅ ΡΠ»ΠΎΠ²ΠΎ Π·Π°Π΄Π°ΡΡΡΡ ΠΊΠ°ΠΊ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΡ ΡΡΠ½ΠΊΡΠΈΠΈ. </h3> <form action="05.php" method="post" name="dir"> <h4>ΠΠ°Π΄Π°ΠΉΡΠ΅ Π΄ΠΈΡΠ΅ΠΊΡΠΎΡΠΈΡ Π΄Π»Ρ Π²ΡΠ²ΠΎΠ΄Π° ΡΠΏΠΈΡΠΊΠ° ΡΠ°ΠΉΠ»ΠΎΠ²:</h4> <p><lable><input type="radio" name="dir_name" value="first_dir" checked> ΠΠ΅ΡΠ²Π°Ρ Π΄ΠΈΡΠ΅ΡΠΊΠΎΡΠΈΡ</lable></br> <p><lable><input type="radio" name="dir_name" value="second_dir"> ΠΡΠΎΡΠ°Ρ Π΄ΠΈΡΠ΅ΡΠΊΠΎΡΠΈΡ</lable></br> <p><lable><input type="radio" name="dir_name" value="third_dir"> Π’ΡΠ΅ΡΡΡ Π΄ΠΈΡΠ΅ΡΠΊΠΎΡΠΈΡ</lable></p> <p> <lable>ΠΡΠΊΠΎΠΌΠΎΠ΅ ΡΠ»ΠΎΠ²ΠΎ: </br><input type="text" name="search" value="ΠΎΠ΄ΠΈΠ½"></lable> <input type="submit" value="ΠΠΎΠΈΡΠΊ" /> </p> </form> <?php //Π€ΡΠ½ΠΊΡΠΈΡ ΡΠ΄Π°Π»Π΅Π½ΠΈΡ ΡΠ»ΠΎΠ² ΠΈΠ· ΡΠ°ΠΉΠ»Π° function displayFilesListSearch ($dir, $search) { $files = glob($dir."/*.txt"); //ΠΠ°Ρ
ΠΎΠ΄ΠΈΠΌ Π²ΡΠ΅ ΡΠ΅ΠΊΡΡΠΎΠ²ΡΠ΅ ΡΠ°ΠΉΠ»Ρ Π² Π·Π°Π΄Π°Π½Π½ΠΎΠΉ Π΄ΠΈΡΠ΅ΠΊΡΠΎΡΠΈΠΈ foreach ($files as $file){ //echo "$search </br>"; if (preg_match("/\s[$search]\s/ui" ,file_get_contents($file))){ echo $file; }; }; }; //Π£ΡΠ»Π²ΠΎΠΈΡ Π²ΡΠΏΠΎΠ»Π΅Π½ΠΈΡ if (array_key_exists('dir_name', $_POST) && strlen($_POST['dir_name'])){ displayFilesListSearch($_POST['dir_name'], $_POST['search']); }; ?> </body> </html> PS The file in the appropriate directory is created and the search word is present there, checked. Maybe it's in the encoding? In the file set utf-8 without BOM.