fork download
  1. <?php
  2.  
  3. $source = <<< DATA
  4. /**
  5. *
  6. */
  7. class Test {
  8.  
  9.   /**
  10.   * Some very important comment here
  11.   */
  12.   public const LOL = "damn";
  13.  
  14.   /**
  15.   * Yet another,
  16.   * potentially multiline comment
  17.   */
  18.   private const TEST = 5;
  19.  
  20.   public const plop = "dong";
  21. }
  22.  
  23. /**
  24. *
  25. */
  26. class Test2 {
  27.  
  28.   /**
  29.   *
  30.   */
  31.   public const LOL2 = "damn";
  32.  
  33.   /**
  34.   *
  35.   */
  36.   private const TEST2 = 5;
  37.  
  38.   public const plop2 = "dong";
  39. }
  40. DATA;
  41.  
  42. $regex_class = '~
  43. class\h*(?P<classname>\w+)[^{}]*
  44. (\{
  45. (?:[^{}]*|(?2))*
  46. \})
  47. ~x';
  48.  
  49. $const_class = '~
  50. ^\h*
  51. (?:(?P<comment>\Q/*\E(?s:.*?)\Q*/\E)(?s:.*?))?
  52. (?:public|private)\h*const\h*
  53. (?P<key>\w+)\h*=\h*(?P<value>[^;]+)
  54. ~mx';
  55.  
  56. $clean_comment = '~^\h*/?\*+\h*/?~m';
  57.  
  58. preg_match_all($regex_class, $source, $matches, PREG_SET_ORDER);
  59.  
  60. foreach ($matches as $match) {
  61. preg_match_all($const_class, $match[0], $constants, PREG_SET_ORDER);
  62. foreach ($constants as $constant) {
  63. $comment = trim(preg_replace($clean_comment, '', $constant["comment"]));
  64. echo "Class: {$match["classname"]}, Constant Name: {$constant["key"]}, Constant Value: {$constant["value"]}, Comment: $comment\n";
  65. }
  66. }
  67. ?>
Success #stdin #stdout 0.02s 24144KB
stdin
Standard input is empty
stdout
Class: Test, Constant Name: LOL, Constant Value: "damn", Comment: Some very important comment here
Class: Test, Constant Name: TEST, Constant Value: 5, Comment: Yet another,
potentially multiline comment
Class: Test, Constant Name: plop, Constant Value: "dong", Comment: 
Class: Test2, Constant Name: LOL2, Constant Value: "damn", Comment: 
Class: Test2, Constant Name: TEST2, Constant Value: 5, Comment: 
Class: Test2, Constant Name: plop2, Constant Value: "dong", Comment: