How to parse doc comments

守給你的承諾、 提交于 2019-12-13 07:00:58

问题


I'm using reflection to get the doc comment from a method: http://www.php.net/manual/en/reflectionclass.getdoccomment.php, which looks like this:

   /** 
    * Method description
    *
    * @param  array $foo    Bla bla
    * @return string        More bla bla
    */

How can I parse this string into something that I can work with? I need to extract the "Method description" text from it. The other stuff is not important to me, because I can use other reflection methods to get the parameters etc.


回答1:


trim(str_replace(array('/', '*'), '', substr($rc->getDocComment(), 0, strpos($rc->getDocComment(), '@'))));

Assuming the comments are always in that format.




回答2:


I've not had much experience with parsing comments, however, treating this as a string, what I'd do is:

  1. Explode by new lines:
  2. trim spaces and * out

Something like this:

<?php

$string = "   /** 
    * Method description
    *
    * @param  array $foo    Bla bla
    * @return string        More bla bla
    */";

$parts = explode("\n",$string);
$comment = trim($parts[1]," *");
echo $comment; // will echo "Method description"

However, probably isn't ideal as the description might be on more than one line.



来源:https://stackoverflow.com/questions/11461800/how-to-parse-doc-comments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!