note 96431 modified in reflectionclass.getdoccomment by danbrown

From: Date: Fri, 26 Feb 2010 19:06:58 +0000
Subject: note 96431 modified in reflectionclass.getdoccomment by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-166402@lists.php.net to get a copy of this message
The code getDocComment() is not as effective as it seems, a method with a well-crafted regular expression, can solve some problems that this method does not address, for example: Some comments that begin with "/ *" will not be returned in a file too extensive. The method below shows how you can use a regular expression to get better results. This code snippet captures the comments in a file. "Php" and replaces it with an empty string, ie "cut" the comments of a class: <?php public function getComments() { $expr = "/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/"; $filename = $this->fileDir; //file directory $file = fopen($filename, "r"); $length = filesize($filename); $comments = fread($file, $length); preg_match_all($expr, $comments, $matchs); //capture the comments foreach($matchs[0] as $id => $variable){ $comments = str_replace($variable,'',$comments); // replace the scores of empty } fclose($file); $file = fopen($filename, "w"); $file = fwrite($file, $comments); } ?> --was-- The code getDocComment() is not as effective as it seems, a method with a well-crafted regular expression, can solve some problems that this method does not address, for example: Some comments that begin with "/ *" will not be returned in a file too extensive. The method below shows how you can use a regular expression to get better results. This code snippet captures the comments in a file. "Php" and replaces it with an empty string, ie "cut" the comments of a class: public function getComments() { $expr = "/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/"; $filename = $this->fileDir; //file directory $file = fopen($filename, "r"); $length = filesize($filename); $comments = fread($file, $length); preg_match_all($expr, $comments, $matchs); //capture the comments foreach($matchs[0] as $id => $variable){ $comments = str_replace($variable,'',$comments); // replace the scores of empty } fclose($file); $file = fopen($filename, "w"); $file = fwrite($file, $comments); } http://php.net/manual/en/reflectionclass.getdoccomment.php

« previous php.notes (#166402) next »