0

I'm struggling to mach everything inside a PHP comment block using RegExr.

i.e.

/**
 * MATCH EVETHING HERE
 */

So basically, I want to match everything starting straight after /** and ending straight before */.

Can someone please provide me with the appropriate RegExr for that.

Thanks in advance

EDIT: solved, its /\/\/*/*(.*?)*//s

Taha
  • 381
  • 1
  • 5
  • 12
  • If this a DocBlock, e.g. if it is written before a function or method, you can use the Reflection API to fetch it. – Gordon Sep 08 '11 at 15:38

2 Answers2

1
preg_match_all('~/**(.*?)*/~ims', $from, $to); 

foreach($to[1] as $contents){
    //$contents contain comment (without /** */) 
}
genesis
  • 50,477
  • 20
  • 96
  • 125
  • Thats for your help buddy! but thats not quite what I was looking for, see, I want to match everything inside the comment block as **one** string. One of my followers on twitter gave me this and worked like a charm `/\/\/*/*(.*?)*//s`. Thanks again :). – Taha Sep 08 '11 at 15:48
  • @Taha: echo $contents."
    "; will do the stuff
    – genesis Sep 08 '11 at 16:02
0

If you want to catch the comment of an class you can use the reflector mechanisms of PHP using the ReflectionClass::getDocComment() method.

Example class file:

/** 
* A test class
*
* @param  foo bar
* @return baz
*/
class TestClass { }

Reflection class usage:

$rc = new ReflectionClass('TestClass');
var_dump($rc->getDocComment())

Outputs:

string(55) "/** 
* A test class
*
* @param  foo bar
* @return baz
*/"

Hint:
On the ReflectionClass Manual page there is a good comment mentioning this reg exp:

    $expr = "/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/"; 
powtac
  • 40,542
  • 28
  • 115
  • 170