Page 1 of 1

find text between to characters

Posted: Wed Oct 22, 2008 10:57 am
by inet411
I'm trying to get a list of all the functions in a text file.
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 ) )
These are my desired results ..sortof.. I can just grab $matches[1][0] and $matches[1][1] or loop through the array etc...
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.

Re: find text between to characters

Posted: Wed Oct 22, 2008 12:12 pm
by GeertDD
This regex would probably do the job:

Code: Select all

~\bfunction\s++([_a-z][_a-z0-9]*+)\s*+\(~i
However, be aware that that regex is far from perfect. Depending on the target string you are working with, you may need to write some kind of parser (which takes PHP comments and strings into account, etc.) in order to really get a list of functions.

Also, I don't think there is something like get_declared_classes() but then for functions, but you may want to look into that further.

Re: find text between to characters

Posted: Wed Oct 22, 2008 12:34 pm
by inet411
GeertDD wrote:This regex would probably do the job:

Code: Select all

~\bfunction\s++([_a-z][_a-z0-9]*+)\s*+\(~i
However, be aware that that regex is far from perfect. Depending on the target string you are working with, you may need to write some kind of parser (which takes PHP comments and strings into account, etc.) in order to really get a list of functions.

Also, I don't think there is something like get_declared_classes() but then for functions, but you may want to look into that further.
Thank you I will try that.
And yes there is a function_exists function but I'm actually reading scripts (as text files) into strings and parsing out string data. I'm not actually running the scripts and checking if the functions are being used etc...
Thanks again for taking the time, I will try the regex you posted and post back with results.