1

The situation is next:

I have php file, which parses a web-page. on that web-page is a phone number and it's digits are mixed with each other. The only way to put each digit on the correct place is to use some JS functions (on the client side). So, when I execute that php file in linux console, it gives me all that I need, except js function's result (no wonder - JavaScript is not a server-side language). So all I see from JS - only a code, that I have written.

The question: can I execute js files via php and how?

5
  • 1
    Im almost positive this is not possible. Ran into something similar a month back and basically, a browser is required to run JS. Commented Sep 30, 2010 at 15:18
  • Or a scripting host like those provided in Windows for VBS and JS. However, I don't recommend it. Commented Sep 30, 2010 at 15:20
  • I don't know if its impossible (I believe in "Impossible is nothing"), but it sure ain't a straight arrow. Commented Sep 30, 2010 at 15:26
  • @Ascherer: The JavaScript engines can be used without the rest of the browser. Commented Sep 30, 2010 at 15:29
  • "The only way to put each digit on the correct place is to use some JS functions (on the client side)." That's an... interesting conclusion. Why can't you use PHP to reorder them? Commented Sep 30, 2010 at 17:24

8 Answers 8

2

Results of a quick google search (terms = javascript engine php)

  1. J4P5 -- not developed since 2005 [BAD](according to its News)
  2. PECL package spidermonkey
  3. a 2008 post by jeresig points to PHPJS but can't see when it was last updated.

I'm sure you'll find many more links on that google search.

Alternatively:

you say that the digits are scrambled and you need to "unscramble" them using js. Can you code that unscrambling logic into a PHP function and just use it? Will sure save you a lot of trouble, but if learning to use js in php is what you're after, then its a whole different story...

Sign up to request clarification or add additional context in comments.

Comments

1

Zend tutorial: "Using javascript in PHP with PECL and spidermonkey"?

Comments

1

http://en.wikipedia.org/wiki/Comparison_of_Server-side_JavaScript_solutions

Alternatively, PHP has simple functions for executing other programs and retrieving their output (I used this along with GPG once to create a PHP file manager which could live-encrypt files as you were uploading them and live-decrypt as you were downloading them)

Using those functions along with http://code.google.com/p/v8/ you should be able to interpret any javascript.

Comments

0

Not unless you know someone who's implemented a Javascript engine in PHP.

In other words, probably not.

Comments

0

Without some sort of browser emulation or passing the unparsed js off to a server side implementation of javascript (maybe node.js?), you won't be able to execute it.

However, does the page use the same js function to unscramble the phone number every time? You should be able to read the incorrect digits and shuffle them with PHP.

Comments

0

If you're prepared to do a bit of work building your own JS runtime to work with it, Tim Whitlock has written a javascript tokenizer and parser in pure PHP

2 Comments

You'll still have to write a js execution engine if you use jParser. Things like scope, memory, closures et al are not dealt with by the parser
I did say "a bit of work" :) but it does provide a starting point for a competent s/w engineer
0

node.js is server-side... but full JS :) no PHP in it so I don't it answer your needs...

Anyway, here is an example : a chat in JS both client & server-side : http://chat.nodejs.org/

Plus, not every host allows you to use the v8 engine...

Comments

0

If you have Javascript data objects, and you need to convert them to/from PHP arrays, that's quite easy using PHP's json_encode() and json_decode() functions.

But actually running Javascript code? No. You can't. You might be able to find a JS interpreter written in PHP (a few other answers have pointed a links that may or may not help you here), or more likely execute the JS using a stand-alone JS interpreter on your server which you call out to from PHP. However if the JS code includes references to the browser's DOM (which is highly likely), that's a whole other set of issues which will almost certainly make it impossible.

Given the way you describe the question, I'd say the easiest solution for you would just be to re-implement the JS code as PHP code; it's unlikely that all the work arounds being suggested would be appropriate for what sounds like a fairly simple bit of utility code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.