0

Good day. Trying to parse the text from the "script" tag and convert it to "JSON". So, the $ arSlider variable is perfectly converted to json, but $ arOffers is not converted, but the string is output. There are comments in the code. What am I doing wrong?

Sample value of $arOffers = {"1441":{"ARTIKUL":"01001","PROPS":[{"VALUE":"Клинок - Полированный","NAME":"Покрытие\\цвет клинка "},{"VALUE":"Рукоять - Эластрон","NAME":"Рукоятка"},{"VALUE":"Чехол - Натуральная кожа со вставкой из АБС пластика","NAME":"Чехол"}]},"1442":{"ARTIKUL":"01003","PROPS":[{"VALUE":"Клинок - Стоунвош черный","NAME":"Покрытие\\цвет клинка "},{"VALUE":"Рукоять - Эластрон","NAME":"Рукоятка"},{"VALUE":"Чехол - Натуральная кожа со вставкой из АБС пластика","NAME":"Чехол"}]}}

include('/inc/simple_html_dom.php');
$url = 'Already to nothing, the problem is solved!';

$html = file_get_html($url);
foreach ($html->find('script') as $el) {
    if (stristr($el->outertext, "arSlider") !== false) {
        $script = str_replace("'", '"', trim(stristr($el->outertext, "arSlider")));
        $script = explode("     arOffers = ", $script);
        $arSlider = str_replace("arSlider = ", "", $script[0]);
        $arSlider = str_replace(";", "", $arSlider);
        $arOffers = str_replace("; ", "", $script[1]);
        $arOffers = str_replace("</script>", "", $arOffers); //Add

        print_r(json_decode($arSlider));
        print_r(json_decode($arOffers));
        //It's working! )))
    }
}

UPDATE

The problem showed me var_dump(json_encode($ arOffers)); The line did not show the closing tag </script> Added replace and it worked! )))

mujuonly
  • 11,370
  • 5
  • 45
  • 75
RAPOS
  • 99
  • 1
  • 2
  • 10
  • whats' the actual value of $arOffers ? – Nikhil G Feb 18 '19 at 12:36
  • Please show us what `$arOffers` actually contains. You can also use [json_last_error_msg()](http://php.net/manual/en/function.json-last-error-msg.php) for more debugging. – M. Eriksson Feb 18 '19 at 12:38
  • actual contains – RAPOS Feb 18 '19 at 12:40
  • {"1441":{"ARTIKUL":"01001","PROPS":[{"VALUE":"Клинок - Полированный","NAME":"Покрытие\\цвет клинка "},{"VALUE":"Рукоять - Эластрон","NAME":"Рукоятка"},{"VALUE":"Чехол - Натуральная кожа со вставкой из АБС пластика","NAME":"Чехол"}]},"1442":{"ARTIKUL":"01003","PROPS":[{"VALUE":"Клинок - Стоунвош черный","NAME":"Покрытие\\цвет клинка "},{"VALUE":"Рукоять - Эластрон","NAME":"Рукоятка"},{"VALUE":"Чехол - Натуральная кожа со вставкой из АБС пластика","NAME":"Чехол"}]}} – RAPOS Feb 18 '19 at 12:40
  • 1
    Please update you question with the content instead of in a comment – M. Eriksson Feb 18 '19 at 12:40
  • Mr Gandhi, changes and changes in JSON – RAPOS Feb 18 '19 at 12:42
  • PHP's `json_decode()` also only works with UTF-8 strings. – M. Eriksson Feb 18 '19 at 12:46
  • Isn't she UTF 8? – RAPOS Feb 18 '19 at 12:56

1 Answers1

3

If the output with "Покрытие\\цвет клинка " is the string representation of $arOffers then the issue is in "Покрытие\\цвет клинка" (twice in that string).

\ is an escape character both in PHP and json.

I don't know where the content of $arrOffers comes from, but try:

$arOffers = str_replace('\\', '\\\\', $arOffers); and then json_decode:

php > $a = '{"a": "Покрытие\\цвет клинка"}';
php > var_dump($a); // string(47) "{"a": "Покрытие\цвет клинка"}"
php > var_dump(json_decode($a)); // NULL
php > var_dump(json_last_error_msg()); // string(12) "Syntax error"                                                                                                              
php > $a = str_replace('\\', '\\\\', $a);
php > var_dump($a); // string(48) "{"a": "Покрытие\\цвет клинка"}"
php > var_dump(json_decode($a));
object(stdClass)#4 (1) {
  ["a"]=>
  string(38) "Покрытие\цвет клинка"
}
php > var_dump(json_last_error_msg()); // string(8) "No error"

See this answer https://stackoverflow.com/a/32057601/5537425 for more info.

blahy
  • 1,294
  • 1
  • 8
  • 9
  • Unfortunately for the full contents of the variable, this solves the problem. ((( $arOffers = {"1441":{"ARTIKUL":"01001","PROPS":[{"VALUE":"Клинок - Полированный","NAME":"Покрытие\\цвет клинка "},{"VALUE":"Рукоять - Эластрон","NAME":"Рукоятка"},{"VALUE":"Чехол - Натуральная кожа со вставкой из АБС пластика","NAME":"Чехол"}]},"1442":{"ARTIKUL":"01003","PROPS":[{"VALUE":"Клинок - Стоунвош черный","NAME":"Покрытие\\цвет клинка "},{"VALUE":"Рукоять - Эластрон","NAME":"Рукоятка"},{"VALUE":"Чехол - Натуральная кожа со вставкой из АБС пластика","NAME":"Чехол"}]}} – RAPOS Feb 19 '19 at 05:27
  • Спасибо за var_dump ))))))) – RAPOS Feb 19 '19 at 06:28