0

I have PHP code that read from text file and allow the user to search for a specified string.

The system work perfect on English text but when it comes to Arabic text the system doesn't display anything.

I know that the issue is in the UNICODE but I do not know how to fix it.

content of the file :

سيلان الطاقة الحرارية من البيئة وإليها. وعادة ما يطلق هذا الاسم على ظاهرة ارتفاع درجات حرارة الأرض عن معدلها الطبيعي. وقد ازداد المعدل العالمي لدرجة حرارة الهواء عند سطح الأرض ب0.74 ± 0.18 °C خلال المائة عام المنتهية سنة 2005. وحسب اللجنة الدولية لتغير المناخ(IPCC) فان "أغلب الزيادة الملحوظة في معدل درجة الحرارة العالمية منذ منتصف القرن العشرين تبدو بشكل كبير نتيجة لزيادة غازات الاحتباس الحراري (غازات البيت الزجاجي) التي تبعثها النشاطات التي يقوم بها البشر.

تخترق أغلب أشعة الشمس الغلاف الجوي لتصل إلى سطح الأرض وأغلب تلك الأشعة تقع في الطول الموجي المرئي (ضوء الشمس الذي تراه العين) لذلك يحدث تسخين لسطح الأرض أثناء النهار، ولا تستطيع الأرض امتصاص الطاقة الشمسية بالكامل، ولو حدث ذلك فستصل حرارة الأرض إلى درجة الانصهار، وتكون نهاية الحياة عليها.

تسخن الأرض لامتصاص حرارة الشمس، ثم يعيد سطح الأرض إشعاع الطاقة الممتصة مرة أخرى إلى الغلاف الجوي، وعند غياب أي تأثيرات معقدة يحدث اتزان حراري. وفي هذه الحالة تكون درجة حرارة سطح الأرض نحو – 2.3 درجة مئوية، وتكون الطاقة التي تعيد الأرض إشعاعها على صورة أشعة تحت الحمراء (وهذا المثال مخالف للواقع).

وفي الواقع تعقيدات، حيث يحتجز الغلاف الجوي جزءاً من الأشعة تحت الحمراء؛ لوجود جزئيات ثاني أكسيد الكربون وبخار الماء التي تمتص الجزء الأكبر من الأشعة تحت الحمراء، والجزء الذي لا يتم إمتصاصه يخرج من الغلاف الجوي إلى الفضاء، ويعمل الجزء الممتص من الأشعة تحت الحمراء في الغلاف الجوي على رفع درجة الحرارة.[1]

php version 7.1.11

i am using Xampp with apache server

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;
 }
}

fclose($myFileLink);
//echo $myFileContents; 
?>

<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
  • Just add u modifier after the i so it can handle Unicode correctly – Mike Doe Dec 26 '17 at 21:16
  • i add u beside like this **/iu** but still nothing happen – Rany Fahed Dec 26 '17 at 21:20
  • How about your SEARCH page. What is it like? – halojoy Dec 26 '17 at 21:21
  • @halojoy what do you mean my SEARCH page ? there is no search page – Rany Fahed Dec 26 '17 at 21:24
  • no one have an answer ? – Rany Fahed Dec 26 '17 at 21:35
  • 1
    @RanyFahed i tried your code with [this text](http://generator.lorem-ipsum.info/_arabic) inside the txt file, and yes it works good in my machine. You need to post more details, like the string you are searching, the content of your text file, php version, web server in use. Also, you looked in your error.log if there is something – RDev Dec 26 '17 at 22:00
  • @RDev I RECHECK the type of text file its UTF-8 and still nothing is displayed either in the error.log file. i wiil edit my question and add more information – Rany Fahed Dec 27 '17 at 04:51

3 Answers3

0

Probably your file is not in utf-8 if you work on windows. Try this :

$myFileContents = utf8_encode(fgets($myFileLink));

Refer to this thread for more information and other solutions: Working with files and utf8 in PHP

USER249
  • 1,080
  • 7
  • 14
0

Its because your form posting is in unicode character, that need to be in utf-8 if your file contains utf-8 encoding

you can check the encoding using following

echo mb_detect_encoding($search);
echo mb_detect_encoding("حلو أل إز جود");

Setting form with utf-8

<html>
    <head></head>
    <meta http-equiv="Content-Language" content="ar-sa">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     <form action="POST.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>

change the encoding accordingly

please add if anything is missing

Mahesh Hegde
  • 1,131
  • 10
  • 12
-3

put this html at top of page

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
halojoy
  • 225
  • 2
  • 7