PHP code to convert PHP to JS

I need PHP code to convert some PHP to JS.

  • Functionality - I use common PHP functions from php.js
  • syntax - ???

The problem is syntax conversion. I do not need the full PHP syntax, mind you; there is no need to support class / declaration definitions. Here is a short checklist of what needs to be converted:

  • "must be" + "(string concat)
  • "->" should be "." (object operator)
  • "::" must be "." (class operator - not required)

Please note that the resulting code is practically independent of the PHP environment, so there is no "what if it uses the PHP class?"

I am not asking for the complete code, but only a hint in the right direction for such a conversion; I was thinking about how to use a car / engine.

If you're wondering why I click code on the user side: I need a dynamic way to change the visibility of certain elements under certain conditions. My plan is to do this without having to execute this side of the code server and have unnecessary ajax calls.

Edit: See people. I do not know how to use AJAX sounds ridiculous for you, but the world does not work on the hype and pleasant design conditions (= ajax). I just can’t allow each user to poll my server from 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 cannot buffer AJAX calls.

Edit 2: I am sure that what I am doing is the best way in my situation. There is no “possibly better” way, so stop posting non-constructive comments. I cannot go into the details of what I already have. Converting from PHP to JS is simply a reduction in user input; we need only one expression, and then convert it depending on what language is needed (in this particular case, from PHP to JS). The conditions for how this will work will not change regardless of whether I describe the system before the API specifications, and flooding the topic with useless (for you) document prototypes will not help at all.

In addition, for those who think that this idea arose after some dream woke up; You know that this was examined between technical development and QA, so please do not deviate from problems with a lack of design.

Edit 3: Examples (PHP source code and expected result):

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

In the last example, do not worry about the context / area, this is correct. Also note that expressions may look strange; this is because they are an expression; one line of code that should return a value, which explains the lack of EOL (;) and the multiple use of returning a boolean value. (exotic things such as executing the backtick operator, PHP tags, echo, die, list, etc.) are not specifically specified)

+1
javascript syntax php parsing
source share
5 answers

Here's a quick and dirty solution that I came across, written in less than 20 minutes (maybe a lot of mistakes), 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; } 

Following:

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

Converted to:

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

Edit: Can't believe this question arose due to time.

Feel free to criticize as you wish. I really don't like it personally.

+2
source share

Ok, let me take a hit on this ...

Screw Modes. I love them, but there is a better way, and it is built-in. Check token_get_all() . It will parse the PHP source as a string and return a list of the same tokens that PHP itself uses (including the favorite T_PAAMAYIM_NEKUDOTAYIM). Then you can completely restore the script source, one token, translating it into Javascript syntax along the way.

 [charles@teh ~]$ php --interactive Interactive shell php > print_r(token_get_all('<?php class Foo { public function bar() { echo "Yikes!"; } } $f = new Foo(); $f->bar(); ?>')); Array ( [0] => Array ( [0] => 368 [1] => <?php [2] => 1 ) [1] => Array ( [0] => 353 [1] => class [2] => 1 ) [2] => Array ( [0] => 371 [1] => [2] => 1 ) [3] => Array ( [0] => 307 [1] => Foo [2] => 1 ) ... 

Although this may be a little redundant, it also uses the same parsing rules that PHP uses, and therefore should have less long-term pain than regular expressions. It also gives you the ability to detect functions that cannot be translated (i.e. things that php-js does not support) and reject the translation and / or solve the problem.


In addition, you still have not told us what you are doing and why you are doing it. There are probably more accurate and useful answers. Help us help you by providing us with additional information.

  • You think the survey is unrealistic due to the expected silly number of requests per second. Why are you expecting this number? What does your application do that can cause such conditions?
  • Why do you want to translate PHP code and not write Javascript? You just manipulate the contents of the page a bit, why do you need PHP code to make this decision?
  • Language translation is probably the least straightforward solution to this problem and therefore an amazingly awful idea. This could not be achieved as a first option. What are your other options and why were they excluded?
+7
source share

Have you tried the Harmony Framework ?

+3
source share

I wrote a tool called php2js that can automatically convert PHP code to javascript. It is not perfect, but it supports the most common PHP functions, including classes, inheritance, arrays, etc. Etc. It also includes and knows about php.js, so code written using the standard php library functions can "just work".

You may find this helpful.

0
source share

The PHP-to-JavaScript tool I created for converting PHP code to JavaScript. His support:

  • Namespaces use
  • Extending class, abstract class, and interfaces
  • constants and define
  • Exceptions and Traps
  • list ()
  • __get __set and __call magic methods
  • and more
-one
source share

All Articles