简体   繁体   English

使用PHP,如何可靠地识别(查找)源字符串中的已知Javascript变量名称?

[英]Using PHP, how do I reliably identify (find) a known Javascript variable name in a source string?

Let's say I have a PHP string that contains the following text, or some other text that is valid Javascript code if put into an interpreter: 假设我有一个包含以下文本的PHP字符串,或者如果包含在解释器中,则是有效的Javascript代码的其他一些文本:

$js_script PHP variable $ js_script PHP变量

var loaded = [];
var loading = [];
var downloading = [];

function load(name)
{
    loading.push(name);
    downloading.push(name);

    // do some asynchronous stuff, with the following callback:
    (function () {
        loaded.push(name);
        downloading = [];
    });
}

I want to replace the variable loading (as in replace the variable name itself in the entire code) to l1 , without using a full javascript parser, and thus producing the following output: 我想将变量loading (例如在整个代码中替换变量名称本身)替换为l1而不使用完整的javascript解析器,从而产生以下输出:

$js_script PHP variable $ js_script PHP变量

var loaded = [];
var l1 = [];
var downloading = [];

function load(name)
{
    l1.push(name);
    downloading.push(name);

    // do some asynchronous stuff, with the following callback:
    (function () {
        loaded.push(name);
        downloading = [];
    });
}

The reasons I assume avoiding using a parser is a good choice: 我认为避免使用解析器的原因是一个不错的选择:

  • they seem to be extreme overkill for this single task, 对于这一项任务,他们似乎是极端的杀伤力,
  • this is the only modification I wish to perform on the input code, 这是我希望对输入代码进行的唯一修改,
  • I explicitly know the variable name I'm going to replace. 我明确知道要替换的变量名称。

I'm not concerned with performance, since the resulting output is to be cached. 我不关心性能,因为要对结果输出进行缓存。 I thought about using regex for this, that is, finding all occurrences of "loading" in the code-string using: 我考虑过使用正则表达式,也就是说,使用以下命令查找代码字符串中所有“正在加载”的情况:

\bloading\b(?=[^"]*(?:"[^"]*"[^"]*)*$)

... which obviously doesn't support apostrophes yet, but other than that, seems to handle the task right so far. ...显然还不支持单引号,但除此之外,到目前为止似乎可以正确处理任务。

Is there something I'm missing? 有什么我想念的吗? I know that JavaScript in wholesale is not a regular language, but how about this part? 我知道批发中的JavaScript并不是一种常规语言,但是这部分呢? Should I forget about using regex, and utilize a parser instead? 我应该忘记使用正则表达式,而改用解析器吗?

If you use preg_replace , it's pretty simple: 如果使用preg_replace ,则非常简单:

$out = preg_replace("/(\W)(loading)(\W)/", "$1l1$3", $in);

An example can be found here . 一个例子可以在这里找到。

EDIT: I did a quick benchmark, and running one replace like this on the file you specified took my computer ~2 μs on average (I put the benchmarking code here ) 编辑:我做了一个快速的基准测试,并且在您指定的文件上像这样运行一次替换平均使我的计算机〜2μs(我将基准测试代码放在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM