开发者

how to read php param version of the file?

is there a way (a php function) to get the version 1.0.0.0 from a fi开发者_如何学编程le?

/**
* @author softplaxa
* @copyright 2011 Company
* @version 1.0.0.0
*/

thanks in advance!


No, there is not a native php function that will extract the version 1.0.0.0 you listed, from a file. However, you can write one:

A. you can parse the file line by line and use preg_match()

B. you can used grep as a system call


$string = file_get_contents("/the/php/file.php");
preg_match("/\*\s+@version\s+([0-9.]+)/mis", $matches, $string);
var_dump($matches[1]);

You can probably write something way more efficient, but this gets the job done.


Here's a tested function that uses fgets, adapted from Drupal Libraries API module:

/**
 * Returns param version of a file, or false if no version detected.
 * @param $path
 *  The path of the file to check.
 * @param $pattern
 *  A string containing a regular expression (PCRE) to match the
 *  file version. For example: '@version\s+([0-9a-zA-Z\.-]+)@'.
 */
function timeago_get_version($path, $pattern = '@version\s+([0-9a-zA-Z\.-]+)@') {
  $version = false;
  $file = fopen($path, 'r');
  if ($file) {
      while ($line = fgets($file)) {
        if (preg_match($pattern, $line, $matches)) {
          $version = $matches[1];
          break;
        }
      }
      fclose($file);
  }
  return $version;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜