I can't seem to figure out how to get a JS array into PHP.
What I have to work with looks like this:
var arrLow = [
{
"e": "495864",
"rank": "8678591",
"rankmove": "<p><img src='up.php?uStyle=144'> UP 495864"
},
{
"e": "104956",
"rank": "-",
"rankmove": "<p><img src='up.php?uStyle=145'> DOWN 1"
},
{
"e": "0",
"rank": "0",
"rankmove": "<p><img src='up.php?uStyle=975'> NEW"
}
]
json_decode and others just return NULL, google only returns some strange way to use serialize() with a HTTP POST from a JS-understanding browser which really can't work here
Does anyone have any clue how :x
==========================================================================
edit: Thanks guys! Didnt know it was so easy
<?php
$json = file_get_contents('24d29b1c099a719zr8f32ce219489cee.js');
$json = str_replace('var arrLow = ','' ,$json);
$data = json_decode($json);
echo $data[0]->e;
?>
You can use json_decode()
for this. The trick is to leave away the var arrLow =
part (so that only the array itself is left). You can assign the value to the php variable $arrLow
like this:
$js = '[ {"e" : "495864", ...';
$arrLow = json_decode($js);
A quick'n'dirty hack to remove the beginning would be to use the strstr()
function.
$js = strstr('var arrLow = [ {..', '[');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With