Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将javascript变量的内容刮到PHP变量?_Php_Javascript_Arrays_Scrape - Fatal编程技术网

将javascript变量的内容刮到PHP变量?

将javascript变量的内容刮到PHP变量?,php,javascript,arrays,scrape,Php,Javascript,Arrays,Scrape,我搜集了一些javascript(使用简单的html和dom),这就是我想到的 $MyScrape的内容 <script type="text/javascript"> var initialInfo = [ [ [29, 30, 'bb1', 'bb2', '02/15/2013 20:00:00', '02/15/2013 00:00:00', 6, 'AT', '1 : 1', '2 : 3', , , '2 : 3'], [

我搜集了一些javascript(使用简单的html和dom),这就是我想到的

$MyScrape的内容

<script type="text/javascript">
var initialInfo = [
    [
        [29, 30, 'bb1', 'bb2', '02/15/2013 20:00:00', '02/15/2013 00:00:00', 6, 'AT', '1 : 1', '2 : 3', , , '2 : 3'],
        [
            [29, 'bb1', 6.91, [
                    [
                        ['pears', [4]],
                        ['kiwis', [20]]
                    ]
                ],
                [
                    [36849, 'abcdefg', 6.24, [
                        [
                            ['apples', [3]],
                            ['oranges', [0]]
                        ]
                    ], 5, 'iff', 29, 2, 88, 'abc', 23, 180, 76]
                ],
                ['4231', [
                    [5, 1],
                    [7, 7]
                ]]
            ]
        ]
    ], 0
];
</script>

有人知道我如何做到这一点吗(最好是简单)?

这可能会让你明白:

function js_array($str, $array_name)
{
    $pattern = "/$array_name ?\[[\s\S]*?\] ?\= ?[\'\"]([\s\S.]*?)[\'\"];/";

    preg_match_all($pattern, $str, $matches);

    $array = (isset($matches[1])) ? $matches[1] : array();

    return $str;
}
$str = js_array($MyScrape, 'initialInfo');
然后我可能会尝试json_解码,正如你提到的


让我知道它是否有效

要将其视为JSON,您必须修复以下几点:

  • JSON使用双引号,而不是单引号
  • …,'1:1','2:3','2:3'],
    中的两个连续逗号不是有效的JSON
  • 您必须删除变量声明(
    var initialInfo=
  • 你必须删掉结尾的分号

您也可以编写自己的解析器,因为此代码只使用数组文本、字符串和数字。

它可能不适用于较旧的浏览器,但您可以使用
JSON.stringify(initialinfo)
,并将其存储到隐藏字段中,然后用PHP将其提取。

好的,下面是我所做的工作

//Cut out the non-json stuff
  $start = strpos($MyScrape,'initialInfo = ')+14;
  $end = strpos($MyScrape,'</script>');
  $data = substr($MatchDetails, $start, ($end-$start));

//Convert the new string to JSON (as it's not quite right)

//Made single quotes into double so that JSON can read it.
  $fixedJSON = str_replace("'", '"', $data);
//change double commas with blank data inside so JSON can read it.
  $fixedCommas = str_replace(",,,", ", 0, 0,", $fixedJSON);
//remove the ending semicolon as JSON can't read it.
  $removedSemiColon = str_replace(";", "", $fixedCommas);

$jsonarray = json_decode($removedSemiColon);

//Now I can actually get stuff out of it...
  echo $row[0][0]; //29
  echo $row[0][1]; //30
  echo $row[0][2]; //bb1
//切掉非json内容
$start=strpos($MyScrape,'initialInfo=')+14;
$end=strpos($MyScrape,”);
$data=substr($MatchDetails,$start,($end-$start));
//将新字符串转换为JSON(因为它不太正确)
//将单引号转换为双引号,以便JSON可以读取。
$fixedJSON=str_replace(“,”,$data);
//更改内有空白数据的双逗号,以便JSON可以读取。
$fixedCommas=str_replace(“,,”,“,0,0,”,$fixedJSON);
//删除结尾分号,因为JSON无法读取它。
$removedSemiColon=str_replace(“;”,“”,$fixedcomas);
$jsonarray=json_decode($removed分号);
//现在我可以从中得到一些东西。。。
echo$row[0][0];//29
echo$row[0][1];//30
echo$行[0][2];//bb1

apos不是引号,数据不是json。您可以预先替换apos,在=和之间裁剪,然后json.decode应该可以工作
//Cut out the non-json stuff
  $start = strpos($MyScrape,'initialInfo = ')+14;
  $end = strpos($MyScrape,'</script>');
  $data = substr($MatchDetails, $start, ($end-$start));

//Convert the new string to JSON (as it's not quite right)

//Made single quotes into double so that JSON can read it.
  $fixedJSON = str_replace("'", '"', $data);
//change double commas with blank data inside so JSON can read it.
  $fixedCommas = str_replace(",,,", ", 0, 0,", $fixedJSON);
//remove the ending semicolon as JSON can't read it.
  $removedSemiColon = str_replace(";", "", $fixedCommas);

$jsonarray = json_decode($removedSemiColon);

//Now I can actually get stuff out of it...
  echo $row[0][0]; //29
  echo $row[0][1]; //30
  echo $row[0][2]; //bb1