用于将PHP转换为JS的PHP代码 [英] PHP code to convert PHP to JS

查看:76
本文介绍了用于将PHP转换为JS的PHP代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些PHP代码将一些PHP转换为JS。

I need some PHP code to convert some PHP into JS.


  • 功能 - 我正在使用来自 php.js

  • 语法 - ???

问题在于转换语法。我不需要完整的PHP语法,请注意;不需要支持类定义/声明。以下是需要转换的小清单:

The issue is converting the syntax. I don't need full PHP syntax, mind you; no need for supporting class definitions/declarations. Here's a small checklist of what needs conversion:


  • 。应该是+(字符串连字)

  • -​​ >应该是。 (对象运算符)

  • ::应为。 (类操作符 - 不是真的需要)

请注意,生成的代码几乎与PHP环境无关,所以没有什么如果它使用PHP类?。

Please note that the resulting code is pretty much independent of the PHP environment, so no "what if it uses a PHP class?".

我不是要求完整的代码,只是提示这种转换的正确方向;我正在考虑使用状态机/引擎。

I'm not asking for full code, just a tip on the right direction to this kind of conversion; I was thinking about employing a state machine/engine.

如果你对我为什么要向用户方推送代码感到好奇:我需要一种动态的方式来在某些条件下改变某些元素的可见性。我的计划是在不必执行此代码服务器端并进行不必要的ajax调用的情况下执行此操作。

If you're curious as to why I'm pushing code to the user side: I need a dynamic way to change visibility of certain elements given certain conditions. My plan is to do this without having to execute this code server side and having unnecessary ajax calls.

编辑:查看人员。我知道没有使用AJAX听起来很荒谬,但世界并不适用于炒作和听起来不错的设计条件(= ajax)。我根本无法承受每个用户每秒轮询我的服务器5到10次,只是为了让我的服务器返回是或否的答案。请记住,切换是异步的,我无法缓冲AJAX调用。

Look people. I know not using AJAX sounds ludicrous to you but the world doesn't work on hype and nice-sounding design conditions (=ajax). I simply can't afford each single user polling my server 5 to 10 times per second just for my server to return a "yes" or "no" answer. Keep in mind that switching is asynchronous, and I can't buffer the AJAX calls.

编辑2:我确定我是什么在我的情况下,做是最好的方式。没有可能更好的方式,所以退出发表非建设性的评论。我不能比我已经有更多细节了。从PHP代码到JS的转换只是缩短用户输入的问题;我们只需要一个表达式,然后将其转换为必要的语言(在这种特殊情况下,从PHP到JS)。无论我如何将系统描述为API规范,并且使用无用的(为您)原型文档淹没主题,这将无法改变的条件

Edit 2: I am sure what I'm doing is the best way in my situation. There is no "possibly better" way, so quit posting non-constructive comments. I can't get into any more detail than I have so already. The conversion from PHP code to JS is simply a matter of shortening user input; we only need one expression, then convert it to whichever language is necessary (in this particular case, from PHP to JS). The conditions on how this works will not change regardless if I describe the system down to the API specs, and inundating the topic with useless (for you) prototype docs will not help at all.

此外,对于那些认为这个想法来自醒来后形成一些梦想的人;知道这已经在技术开发和质量保证之间进行了审核,所以请不要偏离设计问题。

Also, for those thinking this idea came after waking up form some dream; know this has been reviewed between technical development and QA, so please do not deviate into inexistent design issues.

编辑3:示例(原始PHP代码和预期输出):

Edit 3: Examples (original PHP code and expected output):


  • (原始) - (已转换)

  • 5 ==test - 5 ==test

  • '$'。(func(12)* 10) - '$'+(func(12)* 10)

  • Fields :: count()== 5 - Fields.count()== 5

  • $ this-> id == 5 - this.id == 5

  • (original) -- (converted)
  • 5=="test" -- 5=="test"
  • '$'.(func(12)*10) -- '$'+(func(12)*10)
  • Fields::count()==5 -- Fields.count()==5
  • $this->id==5 -- this.id==5

关于最后一个例子,不要担心上下文/范围,这是正确的。还要注意表达式可能看起来很奇怪;这是因为他们表达;必须返回值的单行代码,这解释了缺少EOL(;)和多次使用返回布尔值。 (异常的东西,如反引号操作符执行,PHP标记,回声,骰子,列表等...故意遗漏)

About the last example, don't worry about context/scope, it is correct. Also note that the expressions may look weird; this is because they are expression; a single line of code that must return a value, which explains the absence of an EOL (;) and the multiple use of returning a boolean value. (exotic stuff like backtick operator execution, PHP tags, echo, die, list, etc.. left out on purpose)

推荐答案

这是我提出的快速而肮脏的解决方案,写在20分钟内(可能有很多错误),但它看起来很有效。

Here's the quick and dirty solution I came up with, written in under 20 minutes (probably lots of bugs), but it looks like it works.

function convertPhpToJs($php){
    $php=str_split($php,1); $js='';
    $str='';                                                                                      // state; either empty or a quote character
    $strs=array('\'','`','"');                                                                    // string quotes; single double and backtick
    $nums=array('0','1','2','3','4','5','6','7','8','9');                                         // numerals
    $wsps=array(chr(9),chr(10),chr(13),chr(32));                                                  // remove whitespace from code
    foreach($php as $n=>$c){
        $p=isset($php[$n-1])?$php[$n-1]:'';
        $f=isset($php[$n+1])?$php[$n+1]:'';
        if($str!='' && $str!=$c){ $js.=$c; continue; }                                        // in a string
        if($str=='' && in_array($c,$strs)){ $str=$c; $js.=$c; continue; }                     // starting a string
        if($str!='' && $str==$c){ $str='';  $js.=$c; continue; }                              // ending a string
        // else, it is inside code
        if($c=='$')continue;                                                                  // filter out perl-style variable names
        if($c==':' && $f==':'){ $js.='.'; continue; }                                         // replace 1st of :: to .
        if($p==':' && $c==':')continue;                                                       // filter out 2nd char of ::
        if($c=='-' && $f=='>'){ $js.='.'; continue; }                                         // replace 1st of -> to .
        if($p=='-' && $c=='>')continue;                                                       // filter out 2nd char of ->
        if($c=='.' && (!in_array($p,$nums) || !in_array($f,$nums))){ $js.='+'; continue; }    // replace string concat op . to +
        if(in_array($c,$wsps))continue;                                                       // filter out whitespace
        $js.=$c;
    }
    return $js;
}






以下内容:


The following:

$window->alert("$".Math::round(450/10));

转换为:

window.alert("$"+Math.round(450/10));

编辑:无法相信这个问题引起的所有大惊小怪所花费的时间。

Can't believe all the fuss this question caused compared to the time taken.

随意批评。我个人并不喜欢它。

Feel free to criticize at will. I don't actually like it much personally.

这篇关于用于将PHP转换为JS的PHP代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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