Help - Search - Member List - Calendar
Full Version: [PHP] Regex problem
WorkTheWeb Forums > Webmaster Resources > PHP Help
Support our Sponsors!
Fluffy Convict
In my school's website, I've found a regex that scans files for
functions which name's end with "_ding". On a match, it scans the
JavaDoc style comment of the function and filers out any @user comments.

Perhaps the following example will explain this better:

---------------------------- PHP: -----------------------------
preg_match_all(
";
/** #Comment start
(?P<Comment>.*?) #Comment contents
*/ #End of the comment
s*
functions+(?P<Function>[a-z0-9_]*_ding) #Function name
;xim",
$file,
$matches,
PREG_SET_ORDER
);
---------------------------------------------------------------

Let's assume $file is scanned with this regex and $file contains the
following function:

/**
* @description This is for internal use
* @user This is to inform the user
*/
function foo_ding() {
return 'foo!';
}

Then the output of the rexex would be "This is to inform the user". Now
my problem. It's really quite simple I imagine for somebody with more
regex experience than I have. I now want to match functions that START
with ding_ and not end with it. So the regex should match

/**
* @user Inform the user
*/
function ding_foo() {
return 'foo you too!'
}

Who knows how to rebuild the regex? I've tried the code here under, but
It failt to return the whole function name. $matches['Function'] returns
"ding_f" instead of "ding_foo". Who can help me rewrite this regex
properly? Your help would be greatly appreciated!

---------------------------- PHP: -----------------------------
preg_match_all(
";
/** #Comment start
(?P<Comment>.*?) #Comment contents
*/ #End of the comment
s*
functions+(?P<Function>ding_*[a-z0-9_]) #Function name
;xim",
$file,
$matches,
PREG_SET_ORDER
);
---------------------------------------------------------------

Xenophaw
"Fluffy Convict" <[Email Removed]> wrote:
QUOTE
In my school's website, I've found a regex that scans files for functions
which name's end with "_ding". On a match, it scans the JavaDoc style
comment of the function and filers out any @user comments.

Perhaps the following example will explain this better:

---------------------------- PHP: -----------------------------
preg_match_all(
";
/**                  #Comment start
(?P<Comment>.*?)    #Comment contents
*/                    #End of the comment
s*
functions+(?P<Function>[a-z0-9_]*_ding) #Function name
;xim",
$file,
$matches,
PREG_SET_ORDER
);
---------------------------------------------------------------

Let's assume $file is scanned with this regex and $file contains the
following function:

/**
* @description This is for internal use
* @user This is to inform the user
*/
function foo_ding() {
return 'foo!';
}

Then the output of the rexex would be "This is to inform the user". Now my
problem. It's really quite simple I imagine for somebody with more regex
experience than I have. I now want to match functions that START with
ding_ and not end with it. So the regex should match

/**
* @user Inform the user
*/
function ding_foo() {
return 'foo you too!'
}

Who knows how to rebuild the regex? I've tried the code here under, but It
failt to return the whole function name. $matches['Function'] returns
"ding_f" instead of "ding_foo". Who can help me rewrite this regex
properly? Your help would be greatly appreciated!

---------------------------- PHP: -----------------------------
preg_match_all(
";
/**                  #Comment start
(?P<Comment>.*?)    #Comment contents
*/                    #End of the comment
s*
functions+(?P<Function>ding_*[a-z0-9_]) #Function name

Here it's the mistake. I think you would need ding_[a-z0-9]*. The pattern
you wrote would matches 'ding' followed by 0 or more '_' and a character
between a to z and 0 to 9. If you don't mind to catch any function name like
ding_, i would use ding_[a-z0-9]+, which search for at least 1 character
after the '_'.

I hope this will help you.

QUOTE
;xim",
$file,
$matches,
PREG_SET_ORDER
);
---------------------------------------------------------------


Fluffy Convict
Xenophaw wrote:
QUOTE
"Fluffy Convict" <[Email Removed]> wrote:

In my school's website, I've found a regex that scans files for functions
which name's end with "_ding". On a match, it scans the JavaDoc style
comment of the function and filers out any @user comments.

Perhaps the following example will explain this better:

---------------------------- PHP: -----------------------------
preg_match_all(
";
/**                  #Comment start
(?P<Comment>.*?)    #Comment contents
*/                    #End of the comment
s*
functions+(?P<Function>[a-z0-9_]*_ding) #Function name
;xim",
$file,
$matches,
PREG_SET_ORDER
);
---------------------------------------------------------------

Let's assume $file is scanned with this regex and $file contains the
following function:

/**
* @description This is for internal use
* @user This is to inform the user
*/
function foo_ding() {
return 'foo!';
}

Then the output of the rexex would be "This is to inform the user". Now my
problem. It's really quite simple I imagine for somebody with more regex
experience than I have. I now want to match functions that START with
ding_ and not end with it. So the regex should match

/**
* @user Inform the user
*/
function ding_foo() {
return 'foo you too!'
}

Who knows how to rebuild the regex? I've tried the code here under, but It
failt to return the whole function name. $matches['Function'] returns
"ding_f" instead of "ding_foo". Who can help me rewrite this regex
properly? Your help would be greatly appreciated!

---------------------------- PHP: -----------------------------
preg_match_all(
";
/**                  #Comment start
(?P<Comment>.*?)    #Comment contents
*/                    #End of the comment
s*
functions+(?P<Function>ding_*[a-z0-9_]) #Function name


Here it's the mistake. I think you would need ding_[a-z0-9]*. The pattern
you wrote would matches 'ding' followed by 0 or more '_' and a character
between a to z and 0 to 9. If you don't mind to catch any function name like
ding_, i would use ding_[a-z0-9]+, which search for at least 1 character
after the '_'.

I hope this will help you.


;xim",
$file,
$matches,
PREG_SET_ORDER
);
---------------------------------------------------------------




Thanx, Xenophaw! That really dit the trick. I'm not quite there with my
regex knowledge...but practice makes perfect :)


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.