2017-05-13 26 views
0

我正在用PHP開發。我只有以下語句:奇怪的Linux文件寫作

<?php 
$text = "http://www.aecinema.ir/movie/%d8%a7%db%8c%d9%86-%d9%be%db%8c%da%a9%d8%a7%d9%86-2/"; 
$text = urldecode($text); 
$log_file = fopen("log.txt", "w"); 
$text = substr($text, -100000); 
@$text = mb_convert_encoding($text, 'UTF-8', 'OLD-ENCODING'); 
fwrite($log_file, $text); 
fclose($log_file); 
?> 

Windows操作系統下,日誌文件的內容爲預期:

http://www.aecinema.ir/movie/این-پیکان-2/ 

但在Linux(CentOS的)輸出:

http://www.aecinema.ir/movie/این-پیکان-2/ 

這是解碼錯誤。

編輯:腳本運行Cron。我不知道這是否有所作爲。

編輯:我注意到,只有當我使用瀏覽器打開文件時,字符才顯示爲奇怪。當我下載該文件並在Windows中打開時,一切正常。

回答

1

試試這個,希望這會幫助你。你應該啓動一個定義字符集的頭文件爲utf-8以獲得所需的輸出。這樣header('Content-Type: text/html; charset=utf-8');

Try this code snippet here

<?php 

ini_set('display_errors', 1); 
header('Content-Type: text/html; charset=utf-8'); 
$s='http://www.aecinema.ir/movie/%d8%a7%db%8c%d9%86-%d9%be%db%8c%da%a9%d8%a7%d9%86-2/'; 
echo $s = urldecode($s); 

輸出:http://www.aecinema.ir/movie/این-پیکان-2/

解決方案2:Not a good solution than first one

Try this code snippet here

<?php 

ini_set('display_errors', 1); 
echo '<meta charset="UTF-8">'; 
$s='http://www.aecinema.ir/movie/%d8%a7%db%8c%d9%86-%d9%be%db%8c%da%a9%d8%a7%d9%86-2/'; 
echo $s = urldecode($s); 

整個代碼:

<?php 
ini_set('display_errors', 1); 

header("Content-type: text/html; charset=utf-8"); 
$text = "http://www.aecinema.ir/movie/%d8%a7%db%8c%d9%86-%d9%be%db%8c%da%a9%d8%a7%d9%86-2/"; 
$text = urldecode($text); 
$log_file = fopen("log.txt", "w"); 
$text = substr($text, -100000); 
fwrite($log_file, $text); 
fclose($log_file); 
+0

試了一下。同樣的問題沒有錯誤。請參閱我的編輯。 – Mowji

+0

@Mowji你能分享一下你目前使用的代碼嗎? –

+0

對不起。這是一個Linux文件問題。 – Mowji