So I have the file in a string.
$contents.
here is a sample
Code: Select all
$contents = '
function doSomething ($something,$nothing) {
return $something;
}
function doNothing($nothing)
{
//This function does nothing.
}
';So I really want an array of all the function names.
ie
0=>doSomething
1=>doNothing
That would awesome. I'm guessing a regex to find anything between 'function' AND the first occurrence of '('
Any help would be appreciated.
ps. I've been through php.net and non of my preg_match, preg_match_all that I have tried to write have yielded any results except empty arrays.
Note, you may notice that the two functions are written differently:
there is a space after the doSomething and before the (
the { is underneath the doNothing function
This is because all the functions I'm trying to grab may all be written differently.
---
OK after further research and head pounding I was able to come up with this:
Code: Select all
preg_match_all('/function(.*)\(/', $content, $matches);
print_r($matches);
//Results:
Array ( [0] => Array ( [0] => function doSomething ( [1] => function doNothing( ) [1] => Array ( [0] => doSomething [1] => doNothing ) )Anyone have a better way or a way to grab lets say
1.
function doSomething ($something,$nothing) {
return $something;
}
2.
function doSomething ($something,$nothing)
3.
function doNothing($nothing)
{
//This function does nothing.
}
4.
function doNothing($nothing)
Not necessarily in that order or even with one expression.