简体   繁体   中英

encode javascript array in php?

I tried to encode javascript array in php but doesn't work... i don't understand where is the problem:

This is my javascript code

var reports = [
               ["grup",24.5,101.6,"680 C.","680 C.",0,"N"],
               ["vul-Aca",4.501,-9.876,"192 C.","192 C.",0,"N"]
              ];

and i tried to encode in this way:

$file= "jsfolder/array.js"; //here there is only that js array
$file_contents = file_get_contents($file);
$json_string_array = substr($file_contents,strpos($file_contents,'['),-1);
$array_php = json_decode($json_string_array);

but i think that is a mistake :(

thank a lot and sorry for my english

You have to get rid of semicolon from the end of the string too.

php > var_dump(json_decode("[];"));
NULL
php > var_dump(json_decode("[]"));
array(0) {}

PHP has extension to run javascript code in php.

install V8JS extension in php. Thereafter you can do what you are trying. Get the contents from the file.

$file= "jsfolder/array.js"; //here there is only that js array
$file_contents = file_get_contents($file);

V8JS::executestring method then executes the string provided as javascript code. This will return an array as defined in php, and then you can continue operations in php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM