0

I am trying to make an str replace to a json file. for some reason its not working. This is the script:

$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); 

I have tried to create a string that has the exact same content as the json file has and it worked.

$test = '{ "id" : "1405254580565", "title" : "Testing ", "data" : ["157"] }';

$area = array("228", 
 "157"); 
$place = array("bad",
"good");

$change = str_replace($area, $place, $test); 

So why does it work with the string, and not with the json file?

EDIT: Ok I tried to use decoding

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

var_dump(json_decode($json));

But for some reason its not working. But when I try to input the json code manually it works fine

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
Elad Nava
  • 7,746
  • 2
  • 41
  • 61
user3825694
  • 59
  • 1
  • 9

2 Answers2

0

Your probleme

See :

die(var_dump(file_get_contents ("http://www.klh-dev.com/adom/alert/alerts.json")));

The web page alerts.json is not properly encoded. See output :

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)

Solution

You must to clean your string. Remove null character '\0' after all character except with arabic character, and 'ÿþ' on the beginning of string (it's UTF-16 mark).

iconv function see : 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);

See output :

http://jsbin.com/dumon/1 (<= with highlight).

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)

Example : https://eval.in/172185

Community
  • 1
  • 1
user2226755
  • 12,494
  • 5
  • 50
  • 73
0

There is actually a much easier way to read this JSON, by using iconv().

// 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 );
    }
}
Elad Nava
  • 7,746
  • 2
  • 41
  • 61