I have a Switches.js
javascript file, like this:
//GLOBAL switches
//Change the values accordingly
var SWITCHES = {
uber: true, //Uber
social: true, //Social media pulgins
g_charts: true, //Google Charts
g_captcha: true, //Google Captcha
g_analytics: true, //Google Analytics
data_base: true, //Inserts user input data into DataBase
print: true, //Print option
pdf: true, //Download PDF report option
https: true //true for https, false for http
};
This JS file is stored in my server.
Is there a way to get that object and one of those properties into a php variable? I think that should be around the php functions file_get_contents
and json_decode
, but how do I take into account the comments in the JS file and that variable SWITCHES
in particular?
$file_content = file_get_contents("Switches.js");
//some stuff
$http_switch = $SWITCHES[https];
AJAX nor jQuery are an option because this is to be run purely server side.
Since the Switches.js
is JavaScript and not JSON, you can't use json_decode()
. Instead you should convert it to JSON and decode/parse it both client-side and server-side:
In PHP, you'd do:
$data = file_get_contents("Switches.json");
$switches = json_decode($data);
And then client-side you fetch it via AJAX and parse:
$.get("/Switches.json", function (data) {
var switches = JSON.parse(data)
})
Your Switches.json
file would then be (JSON files must not have comments):
{
"uber": true,
"social": true,
"g_charts": true,
"g_captcha": true,
"g_analytics": true,
"data_base": true,
"print": true,
"pdf": true,
"https": true
}
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.