I need to identify the start and end of function definitions (blocks) in Javascript source code, using PHP. I don't need to parse the code, just literally know where the beginning and end of the Javascript code is if I've read it into a PHP string (using file_get_contents() for example, or some other way).
I need to only identify functions defined at the highest level within the global scope of the Javascript code, and also functions defined at that level for JQuery event callbacks, but I need to ignore more embedded functions in the code.
So, e.g.
function my_JS_func()
{
// some code
}
and
$('#button').onclick(click(function(e){
// some code
});
but I need to ignore the $.post callback function in here (but I would pick up func2 as a whole):
function func2()
{
$.post('myURL', {data: mydata}, function(data){
// ignore me
}
}
Obviously I need to identify the text: "function" (unless it's in a string literal for some reason) and I'm assuming I need to keep track of curly braces {} but otherwise, any ideas welcomed!
Many thanks for any suggestions! (Happy to use a PHP lib, 3rd party software or whatever)