使用 PHP 在字符串中搜索 javascript 函数定义?

发布于 2024-08-17 10:25:40 字数 1028 浏览 9 评论 0原文

我使用 Zend_View_Helper_HeadScript 在网页的头部显示 javascript 函数定义,并且如果需要,我对其进行了扩展以从文件中获取 javascript 代码。我想知道的是,假设我获取并输出了 javascript 文件的内容(而不是包括srcs。)我的意思是,我可以搜索“function”,后跟一些函数名称字符,然后搜索“(”,但这并没有考虑到代码中可能的注释。PHP 中是否内置了一个快速函数可以搜索 javascript 函数定义吗?或者其他预先构建的内容?

示例输出:

<script type="text/javascript">

// function testMe() {}

function otherTest() {alert('Just testing!');}

/*

function testMe() {}

*/

</script>

显然 testMe() 尚未真正在这里定义...

为了清楚起见,我正在尝试执行以下操作:

<?php

$htmlScriptElements = $view->headScript()->toString();
$functionName = 'testMe';

$file = '/path/to/testMe.js';
$type = 'text/javascript';
$attrs = array();
$getScript = TRUE;

if (!js_function_definition_found($functionName, $htmlScriptElements)) {

    $view->headScript()->appendFile($file, $type, $attrs, $getScript);

}

headScript 帮助程序是 home-如果 $getScript 为 true,我会使用 file_get_contents() 来获取 js 内容并附加它,而不是使用 url/src 包含 js 文件,我意识到它不会在包含的 js 文件中找到定义;没关系。

I'm using Zend_View_Helper_HeadScript to display javascript function definitions in the head of my webpages, and I extended it to get the javascript code from file if desired. What I'd like to know is if there would be an easy way to search the headScript output (basically, a series of xhtml script elements) for a javascript function definition, assuming I got and outputted the contents of javascript files (rather than including the srcs.) I mean, I could search for "function" followed by some function name characters and then "(" but that doesn't account for possible comments in the code. Is there a quickie function for this that is built into PHP that can search for a javascript function definition? Or something else prebuilt?

Sample output:

<script type="text/javascript">

// function testMe() {}

function otherTest() {alert('Just testing!');}

/*

function testMe() {}

*/

</script>

Obviously testMe() hasn't really been defined here...

For clarity, I'm trying to do something like this:

<?php

$htmlScriptElements = $view->headScript()->toString();
$functionName = 'testMe';

$file = '/path/to/testMe.js';
$type = 'text/javascript';
$attrs = array();
$getScript = TRUE;

if (!js_function_definition_found($functionName, $htmlScriptElements)) {

    $view->headScript()->appendFile($file, $type, $attrs, $getScript);

}

The headScript helper is home-brew; if $getScript is true, I use file_get_contents() to get the js content and append that instead of including a js file using a url/src. I realize that it won't find definitions in included js files; I'm fine with that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

围归者 2024-08-24 10:25:40

我将在这里进行猜测并说您想要的是基于 PHP 的 Parser/Lexer 对于 JavaScript。您可能需要查看 Spidermonkey PECL 扩展,因为它可能会帮助您实现这一点。

I'm going to make a guess here and say what you are wanting is a PHP based Parser/Lexer for Javascript. You may want to take a look at the Spidermonkey PECL extension as it may get you part way there.

不知在何时 2024-08-24 10:25:40

您可以使用 typeof 运算符从 JavaScript 本身测试 JavaScript 函数是否存在,例如,

typeof alert === 'function' // true
typeof foo   === 'function' // false

或者,如果您对类型不感兴趣,您可以执行

if (window.alert) { ... }

window is 的操作您正在查找的函数的范围,例如 document.getElementByIdmyModule.someFn

因此您不必从 PHP 进行检查。如果您想从 PHP 执行此操作,您可以迭代 < em>headscript 占位符 并解析里面的字符串。我不知道有哪个函数可以执行 findAllJavaScripts() 操作,因此它需要一些聪明的正则表达式。

You can test for the existence of a JavaScript function from JavaScript itself with the typeof operator, e.g.

typeof alert === 'function' // true
typeof foo   === 'function' // false

or, if you are not interested in the type, you can do

if (window.alert) { ... }

where window is the scope of the function you are looking for, e.g. document.getElementById or myModule.someFn

So you don't have to check from PHP. If you would want to do this from PHP, you could iterate over the headscript placeholder and parse the strings inside. I am not aware of a function that would do findAllJavaScripts(), so it would require some clever Regex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文