Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-08-2008, 01:23 PM   PM User | #1
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
Regex Help

I'm trying to create a regex to capture the comment blocks in a php file. Specifically blocks like:

PHP Code:
/**
* some text here
* and some more
* etc
*/
function here()
{
}

/**
* some text here
* and some more
* etc
*/ 
Basically a block that starts with /** and ends with */ and can have any text in the middle

I tried this: /\*\*[.\r\n]+\*/ but that didn't work.
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 05-08-2008, 03:49 PM   PM User | #2
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
I got it working but its a bit of a cludge - I used [^~] (on the grounds I wont be using ~ in my comments) instead of [.\r\n], for some reason . wasn't working in a character class.

This is what I'm currently using but I'd still like to fix it to be less cludgy.

/\*\*([^~]*?)\*/
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 05-08-2008, 04:05 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Ah, too much work. Trust me, I tried before php 5 and I hated it I recall.
I'm pretty sure you said your on php 5+ now right? Look into the 'Reflection' class in php, one of them is the ReflectionFunction, which will drag all the info out for you (where comment is one of its own so long as its /** .. */ style - only two stars at the start).
Try that nancy, you'll love all the other stuff it does too!

Edit:
Here's a link directly to ReflectionFunction
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 05-08-2008, 04:05 PM   PM User | #4
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
try /\/\*\*(.|[\r\n])+?\*\//g
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow
shyam is offline   Reply With Quote
Old 05-08-2008, 04:56 PM   PM User | #5
Mwnciau
Regular Coder

 
Join Date: May 2006
Location: Wales
Posts: 820
Thanks: 1
Thanked 82 Times in 79 Posts
Mwnciau is on a distinguished road
If you want it less cludgy you could just use /\*\*.+?\*/ with the s modifier.
Mwnciau is offline   Reply With Quote
Old 05-08-2008, 11:19 PM   PM User | #6
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
As far as I know can't be done with a single regex. A solution, if this help you is that:
PHP Code:
<?php
$texts 
"/**
* some text here
* and some more
* etc
*/
function here()
{
}

/**
* some text here
* and some more
* etc
*/  

/* Another style
 *
 */
"
;

function 
markblock($s){
  
$r preg_split("/\*\//",$s);
  return array(
"cmt" => $r);
}

$comments preg_split("/\/\*\*?\s*/",$texts);
$comments array_map("markblock",$comments);
var_dump($comments);
?>
you can format the comments or sections for example to replace * and spaces/newlines with a single space....

to find and list comment sections
PHP Code:
foreach($comments as $blk){
  echo 
$blk['cmt'][0]."\n--------------\n";

same thing with sections what follow comments
PHP Code:
foreach($comments as $blk){
  echo 
$blk['cmt'][1]."\n--------------\n";
}
?> 
regards
oesxyl is offline   Reply With Quote
Old 05-08-2008, 11:59 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I'm pretty sure when I used this method I did it in a single regexp. Let me think...
PHP Code:
/**
 * Function: MyFunc
 * All that fun stuff here
 */
... 
PHP Code:
...
$matches = array();
preg_match('/\/\*\*(.*)\*\//ms'$inputStream$matches);
$i 0;
foreach (
$matches AS $vals)
{
    
// Yes I see what you're getting at now, this may not work.  Thats ok.
    
if ($i++ == 0)
    {
        continue;
    }
    
// Do whatever you want but you'll need...
    
$vals trim($vals' *');

Edit:
I see what you mean now oesxyl, this may not work. I'm thinking that the resultant will always have too much data in it.


I just can't remember off hand if I used an ungreedy quantifier on it or not, I'm thinking that I did or I should only get one set of comments which would include tons of code in it as well.
This of course was only for doc style comments (/** ... */), with only the two leading stars (just like the reflection, it would fail if there were more)
Reflection usage (if you have it, its so much easier):
PHP Code:
/**
* some text here
* and some more
* etc
*/
function here()
{
}

/**
* some text here
* and some more
* etc
*/ 
Now, you can either use the reflection on an individual function, or collect all that are defined:
PHP Code:
// Lets get all the user defined ones by forcing the script to give it to us:
$myComments = array();
$defined get_defined_functions(); // Built in since php 4.0.4
if (is_array($defined['user']))
{
    foreach (
$defined['user'] AS $userFunc)
    {
        
$reflect = new ReflectionFunction($userFunc);
        
$myComments[$userFunc] = var_export($reflect->getDocComment(), 1);
    }

That should give you an array of your comments with an associative function name in mycomments. There is a whole pile of stuff you can get with Reflection, and its awesome for TDD if you're into that
PHP Code:

<?php
class ReflectionFunction extends ReflectionFunctionAbstract implements Reflector
{
    final private 
__clone()
    public 
void __construct(string name)
    public 
string __toString()
    public static 
string export(string namebool return)
    public 
string getName()
    public 
bool isInternal()
    public 
bool isDisabled()
    public 
bool isUserDefined()
    public 
string getFileName()
    public 
int getStartLine()
    public 
int getEndLine()
    public 
string getDocComment()
    public array 
getStaticVariables()
    public 
mixed invoke([mixed args [, ...]])
    public 
mixed invokeArgs(array args)
    public 
bool returnsReference()
    public 
ReflectionParameter[] getParameters()
    public 
int getNumberOfParameters()
    public 
int getNumberOfRequiredParameters()
}
?>
Been awhile since I've reflected too, so you may need to tweek it a bit.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 05-09-2008, 12:21 AM   PM User | #8
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by Fou-Lu View Post
That should give you an array of your comments with an associative function name in mycomments. There is a whole pile of stuff you can get with Reflection, and its awesome for TDD if you're into that
probably are more solution to this problems. I don't look for some parser extensions for php, for example, .
But all this depends of what NancyJ want to achive. If want to build some CASE for php, probably the best solution is reflection.
If only want to extract the comments block, a documenting system, one solution look like mine or better a already build package like doxygen( I don't know if there is a version for windows but I guess it is).

regards
oesxyl is offline   Reply With Quote
Old 05-09-2008, 12:31 AM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by oesxyl View Post
probably are more solution to this problems. I don't look for some parser extensions for php, for example, .
But all this depends of what NancyJ want to achive. If want to build some CASE for php, probably the best solution is reflection.
If only want to extract the comments block, a documenting system, one solution look like mine or better a already build package like doxygen( I don't know if there is a version for windows but I guess it is).

regards
Agreed. PHP is so expansive I don't think I'll ever get to half of the built in stuff, even with 8 years of usage >.<. I suggest reflection simply because I do so much object work now - so easy to build my documentation up, otherwise I'd be too lazy to document my methods, lol.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:41 PM.


Advertisement
Log in to turn off these ads.