Take the 2-minute tour ×

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?

share|improve this question
Im almost positive this is not possible. Ran into something similar a month back and basically, a browser is required to run JS. –  Ascherer Sep 30 '10 at 15:18
Or a scripting host like those provided in Windows for VBS and JS. However, I don't recommend it. –  Nate Sep 30 '10 at 15:20
I don't know if its impossible (I believe in "Impossible is nothing"), but it sure ain't a straight arrow. –  jrharshath Sep 30 '10 at 15:26
@Ascherer: The JavaScript engines can be used without the rest of the browser. –  Tim Büthe Sep 30 '10 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? –  Powerlord Sep 30 '10 at 17:24
add comment

8 Answers

up vote 2 down vote accepted

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.


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...

share|improve this answer
add comment


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.

share|improve this answer
add comment

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

In other words, probably not.

share|improve this answer
mozilla actually maintains a PECL spidermonkey package for php.. –  jrharshath Sep 30 '10 at 16:08
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
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 –  jrharshath Sep 30 '10 at 16:10
I did say "a bit of work" :) but it does provide a starting point for a competent s/w engineer –  Mark Baker Sep 30 '10 at 16:13
add comment

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...

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer


