你的问题
参见:
die(var_dump(file_get_contents ("http://www.klh-dev.com/adom/alert/alerts.json")));
网页alerts.json未正确编码。参见输出:
string '��{� � � �"�i�d�"� �:� �"�1�4�0�5�2�5�4�5�8�0�5�6�8�"�,� �
�"�t�i�t�l�e�"� �:� �"������ ������ ������ ������ �"�,�
� �"�d�a�t�a�"� �:� �[� � �"����� ���� �2�2�8�"� � �]� � �}� � �' (length=206)
解决方案
你必须清理你的绳子。删除除阿拉伯字符以外的所有字符后的空字符“\0”,
和
字符串开头的“”(它是
UTF-16 mark
).
iconv函数请参见:
Convert UTF-16LE to UTF-8 in php
UTF-16:
PHP File Opening Encoding Problem?
<?php
$web = file_get_contents ("http://www.klh-dev.com/adom/alert/alerts.json");
$str_datakey = join("\0", preg_split("//", '"data" : ['));
$pos_datakey = strrpos($web, $str_datakey) + strlen($str_datakey);
preg_match('#"\0t\0i\0t\0l\0e\0"\0.+"\0(.*)"#', $web, $match);
preg_match('#"\0(.*)"#', $web, $match2, null, $pos_datakey);
// Replace title to replace null bit except title (with arabic char)
$title = $match[1];
$web = str_replace($title, "[HorsSujet]", $web);
// Replace string data to replace null bit except string data (with arabic char)
if (!empty($match2)) {
$match2 = array_slice($match2, 1);
$web = str_replace($match2, "[HorsSujet_data]", $web);
}
$web = str_replace(array('ÿþ', "\0"), "", $web);
$title = iconv("UTF-16LE", "UTF-8", $title);
for ($i = 0; $i < count($match2); $i++) {
$match2[$i] = iconv("UTF-16LE", "UTF-8", $match2[$i]);
}
$web = str_replace("[HorsSujet]", $title, $web);
if (!empty($match2)) {
$web = str_replace(array("[HorsSujet_data]"), $match2, $web);
}
$json = $web;
$array = json_decode($web);
var_dump($match, $match2, $web, $array);
参见输出:
http://jsbin.com/dumon/1
(<=带高光)。
array (size=2)
0 => string '"�t�i�t�l�e�"� �:� �"������ ������ ������ ������ �"' (length=71)
1 => string '����� ������ ������ ������ �' (length=48)
array (size=1)
0 => string '×¢×××£ ×¢×× 228' (length=19)
string '{
"id" : "1405254580568",
"title" : "פ××§×× ××¢×רף ××ª×¨×¢× ××ר×× ",
"data" : [
"×¢×××£ ×¢×× 228"
]
}
' (length=129)
object(stdClass)[1]
public 'id' => string '1405254580568' (length=13)
public 'title' => string 'פ××§×× ××¢×רף ××ª×¨×¢× ××ר×× ' (length=44)
public 'data' =>
array (size=1)
0 => string '×¢×××£ ×¢×× 228' (length=19)
例子:
https://eval.in/172185