解析PHP中的Javascript文件以提取其中定义的数组的最佳方法 [英] Best way to parse a Javascript file in PHP to extract the array defined inside it

查看:104
本文介绍了解析PHP中的Javascript文件以提取其中定义的数组的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Javascript文件,它是从旧版应用程序自动生成的,该旧版应用程序中定义了一个巨大的数组(以及其他一些功能和东西).这个javascript文件针对内容执行搜索,但是随着时间的流逝,它已增长到2Mb以上,这听起来可能并不多,但是每次您要使用此特定Web应用程序进行搜索时,都必须下载此文件.不用说表演是残酷的.我想要一种省力的方法来在js周围放置一个包装器,以便与其在客户端调用js,而不必调用在内容上进行搜索的新php脚本.

I've got a Javascript file which is automatically generated from a legacy app which has a huge array defined in it (and a couple of other functions and stuff). This javascript file performs searches against content, but over time it has grown to over 2Mb, which might not sound much, but you have to download this every time you want to do a search with this particular web app. Needless to say the performance is atrocious. I want a minimal effort way of putting a wrapper around the js so that instead of calling the js on the client side, it calls my new php script which does the search on the content.

每次生成的JS文件的布局都会相同,因此我可以编写一堆特定的修整和拆分,但是后来我想到regexp可能是可行的方法,但是老实说我不确定,所以我想我只会问你可爱的人.

The layout of the generated JS file will be the same each time it is generated, so I could write a bunch of specific trims and splits, but then I was thinking regexp might be the way to go, but to be honest I'm not sure, so I thought I would just ask you lovely people.

样本来源:

Page[0]=new Array("Some text1","More text1","Final Text1","abc.html");
Page[1]=new Array("Some text2","More text2","xyz.html");

如您所见,每个数组行中至少有一个条目,最后一个条目是要搜索的文件的名称.

As you can see, there is at least one entry in each array line, with the final entry being the name of the file being searched for.

无论如何,问题是正则表达式是否最好(如果是,则建议的一些模式会很棒).还是我应该用split等来分割这个.

Anyway, the question is, whether regexp is best (and if so, some suggested patterns would be great). or if I should be splitting this with split, etc.

欢呼

推荐答案

您正在寻找类似的东西.注意我将.js文件作为本地文件,因此我使用file()将其加载到数组中.对于您的实际脚本,如果您的PHP无法在本地访问.js文件,则可能需要file_get_contents().

You are looking for something like this. Note I had the .js file as local so I used file() to load it into array. For your actual script you'll probably need file_get_contents() if your php can't access locally the .js file.

<?php
$lines = file('test.js');

    $pages = array();

    foreach($lines as $line) {
        if(strpos($line, 'new Array') != false) {

            preg_match('/Page\[\d\]\s?\=\s?new Array\((\"(.*)",?\s?\n?)+\);/', $line, $matches);


            $values = preg_split('/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/', $matches[1]);

            $currNo = count($pages);
            $pages[$currNo] = array();

            for($i = 0; $i < count($values); $i++) {
                array_push($pages[$currNo], trim($values[$i], '"'));
            }


        }
    }

    var_dump($pages);

以您的示例为例,结果如下:

For your example the result will be the following:

array(2) {
  [0]=>
  array(4) {
    [0]=>
    string(10) "Some text1"
    [1]=>
    string(10) "More text1"
    [2]=>
    string(11) "Final Text1"
    [3]=>
    string(8) "abc.html"
  }
  [1]=>
  array(3) {
    [0]=>
    string(10) "Some text2"
    [1]=>
    string(10) "More text2"
    [2]=>
    string(8) "xyz.html"
  }
}

享受!

这篇关于解析PHP中的Javascript文件以提取其中定义的数组的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆