I want to return the PHP token_get_all() function as JSON.
I also want token_get_all to pass the token through the token_name() function to get its name.
I have tried various different methods but none produce the results I need.
I'm wanting to use this information in JavaScript, I want to be able to call tokens.tokenName for example.
I think I need something like the following example:
{
"tokenName":"T_COMMENT","tokenValue":"# some comment","tokenLine":"1"
"tokenName":"T_VARIABLE","tokenValue":"$some_variable","tokenLine":"2"
}
I've tried to put the token_get_all() function directly through the json_encode() function, as well as playing around with various arrays and the results are not what I wanted.
This is the latest incarnation of the code:
if (isset($_POST['code']) || (isset($_GET['code']))) {
if (isset($_POST['code'])) {
$code = $_POST['code'];
} elseif (isset($_GET['code'])) {
$code = $_GET['code'];
}
$tokens = array();
$tokenName = array();
$tokenValue = array();
$tokenLine = array();
foreach(token_get_all($code) as $c) {
if(is_array($c)) {
array_push($tokenName, token_name($c[0])); // token name
array_push($tokenValue, $c[1]); // token value
array_push($tokenLine, $c[2]); // token line number
} else {
array_push($tokenValue, $c); // single token, no value or line number
}
}
// put our token into the tokens array
array_push($tokens, $tokenName);
array_push($tokens, $tokenValue);
array_push($tokens, $tokenLine);
// return our tokens array JSON encoded
echo(json_encode($tokens));
}
Thank you,
Ryan
I guess what you actually want to do is generate a list of dictionaries. For that you should prefer ordinary array appending instead of array_push
:
foreach(token_get_all($code) as $c) {
$tokens[] =
array(
"tokenName" => token_name($c[0]),
"tokenValue" => $c[1],
"tokenLine" => $c[2]
);
}
Saves you a few temporary variables and is easier to read. It would give you a result such as:
[
{"tokenName":"T_COMMENT","tokenValue":"# some comment","tokenLine":"1"},
{"tokenName":"T_VARIABLE","tokenValue":"$some_variable","tokenLine":"2"}
]
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.