自动解析PHP以将PHP代码与HTML分开 [英] Automatically parsing PHP to separate PHP code from HTML

查看:83
本文介绍了自动解析PHP以将PHP代码与HTML分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发大型PHP代码库;我想将PHP代码与HTML和JavaScript分开. (我需要在PHP代码上执行多个自动搜索和替换,在HTML上执行不同的自动搜索和替换,在JS上进行不同的自动搜索和替换).是否有一个好的解析器引擎可以为我分离出PHP?我可以使用正则表达式来做到这一点,但它们并不完美.也许我可以在ANTLR中构建一些东西,但是最好是一个已经存在的好的解决方案.

I'm working on a large PHP code base; I'd like to separate the PHP code from the HTML and JavaScript. (I need to do several automatic search-and-replaces on the PHP code, and different ones on the HTML, and different on the JS). Is there a good parser engine that could separate out the PHP for me? I could do this using regular expressions, but they're not perfect. I could build something in ANTLR, perhaps, but a good already existing solution would be best.

我应该明确指出:我不需要或不需要完整的PHP解析器.只需要知道给定的令牌是否为: -PHP代码 -PHP单引号字符串 -PHP双引号字符串 -PHP评论 -不是PHP,而是HTML/JavaScript

I should make clear: I don't want or need a full PHP parser. Just need to know if a given token is: - PHP code - PHP single quote string - PHP double quote string - PHP Comment - Not PHP, but rather HTML/JavaScript

推荐答案

tokenizer 是否内置在PHP本身中?

How about the tokenizer built right into PHP itself?

Tokenizer函数提供了一个 与PHP标记器的接口 嵌入在Zend Engine中.使用 这些功能,您可以编写自己的 PHP源代码分析或修改 工具而不必处理 词汇中的语言规范 级别.

The tokenizer functions provide an interface to the PHP tokenizer embedded in the Zend Engine. Using these functions you may write your own PHP source analyzing or modification tools without having to deal with the language specification at the lexical level.

您在注释中询问是否可以从标记化的输出中重新生成代码-但是您可以将所有空格保留为T_WHITESPACE标记.这是将标记化的输出重新转换为代码的方法:

You ask in the comments whether you can regenerate the code from the tokenized output - yet you can, all whitespace is preserved as T_WHITESPACE tokens. Here's how you might turn the tokenized output back into code:

$regenerated='';

$tokens = token_get_all($code);
foreach($tokens as $idx=>$t)
{
    if (is_array($t))
    {

         //do something with string and comments here?
         switch($t[0])
         {
             case T_CONSTANT_ENCAPSED_STRING:
                  break;
             case T_COMMENT:
             case T_DOC_COMMENT:
                 break;

         }
         $regenerated.=$t[1];


    }
    else
    {
         $regenerated.=$t;
    }
}

这篇关于自动解析PHP以将PHP代码与HTML分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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