解析PHP中的JavaScript数组 [英] Parsing javascript arrays in PHP
问题描述
我似乎无法弄清楚如何获得一个JS数组PHP。
我有什么用看起来像这样工作的:
VAR arrLow = [
{
E:495864,
等级:8678591,
rankmove:&所述p为H.;&下; IMG SRC =up.php uStyle = 144?'> UP 495864
},
{
E:104956,
等级: - ,
rankmove:< P>< IMG SRC ='up.php uStyle = 145?'> DOWN 1
},
{
E:0
等级:0,
rankmove:< P>< IMG SRC ='up.php uStyle = 975?>新建
}
]
json_de code和其他人只是返回NULL,Google只返回到使用序列化()与来自JS-了解浏览器,真的不能在这里工作一个HTTP POST一些奇怪的方式
没有人有任何线索如何:X
=============================================== ===========================
编辑:谢谢你们!不知道它是那么容易的。
< PHP
$ JSON =的file_get_contents('24d29b1c099a719zr8f32ce219489cee.js');
$ JSON = str_replace函数('无功arrLow =','',$ JSON);
$数据= json_de code($ JSON);
回声$数据[0] - > e;
?>
您可以使用 json_de code()
这一点。关键是要放弃使用 VAR arrLow =
部分(这样只有阵列本身就是左)。您可以将值分配给PHP变量 $ arrLow
是这样的:
$ JS ='[{E:495864,......;
$ arrLow = json_de code($ JS);
一个quick'n'dirty黑客删除年初将是使用 的strstr( )
功能。
$ = JS的strstr('VAR arrLow = [{..','[');
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 = [ {..', '[');
这篇关于解析PHP中的JavaScript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!