代码之家  ›  专栏  ›  技术社区  ›  user3825694

将JSON web服务中的UTF-16数据解析为PHP中的字符串

  •  0
  • user3825694  · 技术社区  · 12 年前

    我正在尝试将str替换为json文件。 由于某种原因,它不起作用。 这是脚本:

    $json = file_get_contents ("http://www.klh-dev.com/adom/alert/alerts.json");
    
    $area = array("228", 
     "157"); 
    $place = array("bad",
    "good");
    
    $change = str_replace($area, $place, $json); 
    

    我试图创建一个字符串,该字符串的内容与json文件的内容完全相同,并且工作正常。

    $test = '{ "id" : "1405254580565", "title" : "Testing ", "data" : ["157"] }';
    
    $area = array("228", 
     "157"); 
    $place = array("bad",
    "good");
    
    $change = str_replace($area, $place, $test); 
    

    那么,为什么它可以处理字符串,而不能处理json文件呢?

    编辑: 好的,我尝试使用解码

    $json = file_get_contents ("http://www.oref.org.il/WarningMessages/alerts.json");
    
    var_dump(json_decode($json));
    

    但由于某些原因,它不起作用。 但当我尝试手动输入json代码时,它工作正常

    $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
    
    2 回复  |  直到 9 年前
        1
  •  0
  •   Elad Nava    10 年前

    实际上,通过使用iconv(),有一种更简单的方法来读取此JSON。

    // Query Home Front Command API
    // Accessible only from Israel    
    $json = @file_get_contents( 'http://www.oref.org.il/WarningMessages/alerts.json' );
    
    // Got a response from the API?    
    if ( $json ) {
        // Convert from UTF-16 to UTF-8    
        $json = @iconv( 'UTF-16', 'UTF-8', $json );
    
        // Succeeded?    
        if ( $json ) {
            // Trim JSON of whitespace    
            $json = trim( $json );
    
            // Convert JSON string to object    
            $json = @json_decode( $json );
    
            // Output JSON object           
            var_dump( $json );
        }
    }
    
        2
  •  0
  •   Hors Sujet    9 年前

    你的问题

    参见:

    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