0

I have to extract defined variable and function names from a js code passed as a text. Example:

$js_code = " var MyVar1 = 'val1'; \n var MyVar2 = 'val2' ; \n function MyFunc() { some code }";

Extracted:

$js_vars = array("MyVar1", "MyVar2");
$js_func = array("MyFunc");
Stefan K
  • 47
  • 5
  • no recommendation here, but a quick google search led me to http://timwhitlock.info/jparser/ – Christoph Mar 05 '10 at 16:22
  • 2
    Is it possible for you to send these variables as a JSON object? That would make things far more easy: in that case you could just use `json_decode` on the server. – Marcel Korpel Mar 05 '10 at 17:12
  • I think it isn't possible. I send a textarea value to the server. I don't know a way to transform a string like the example one into JSON encoding/decoding data. – Stefan K Mar 05 '10 at 18:54
  • @Marcel Korpel that's a good idea, i had thought it as well, but assumed there was a reason why not, heh. You're right though, @Stefan K, JSON can't handle a function. – cazlab Mar 05 '10 at 19:00

1 Answers1

3

Have you tried Aptana Jaxer? It's an HTTP/Apache javascript parser.

There's also tools like jParser: http://timwhitlock.info/jparser/ Which is a php library which parses javascript. That might be more like what you need, but only if you're using PHP server-side.

I hope that helps.

cazlab
  • 788
  • 5
  • 17
  • I knew jParser library. Yes, it's more like what I need but I would like to test and another parser. Thanks for the answer. – Stefan K Mar 05 '10 at 16:42