I have a javascript file (let's call it external.js) located at some place on my server with the following content:
var exchange = {
"USD": {
"transfer": 203.49,
"cash": 90.08,
"average": 203.49,
},
"EUR": {
"transfer": 231.16,
"cash": 102.32,
"average": 231.16,
},
"USDCOL": {
"rate": 2625.00,
"ratecash": 2350.00,
"ratetrm": 2500.59,
"trmfactor": 0.05,
"trmfactorcash": 0.06
},
"EURUSD": {
"rate": 1.1359
}
"GOLD": {
"rate": 1203.95
}}
//this code is simplified there is a lot more values
I will like to read that file from PHP (something similar to what is done with external xml files) and split those strings into variables, like this:
$USD_transfer = 203.49;
$USD_cash = 90.08;
$USD_average = 203.49;
$EUR_transfer = 231.16;
$EUR_cash = 102.32;
$EUR_average = 231.16.49;
$GOLD_rate = 1203.95;
Then proceed and execute the rest of my php code.
I tried file_get_contents, reading a section of a file, to extract every variable value one by one, but it is obviously unpractical.
<?php
//Read 6 characters starting from the 166 character to get $USD_transfer
$file = file_get_contents('./external.js', NULL, NULL, 166, 6);
$USD_transfer = $file;
?>
PD: since I am working with a pre-made (unalterable) code that generates (only) that javascript file I can not modify it, if it where my choice I would use a database... but that's not the case.
My coding skills are very limited, can you guys please help me?
json_decode().json_decode()like @JohnConde suggests?