find text between to characters

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
inet411
Forum Newbie
Posts: 11
Joined: Mon Oct 20, 2008 11:14 am

find text between to characters

Post 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.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: find text between to characters

Post 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.
inet411
Forum Newbie
Posts: 11
Joined: Mon Oct 20, 2008 11:14 am

Re: find text between to characters

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