有没有办法(一个 php 函数)从文件中获取 1.0.0.0 版本?
/**
* @author softplaxa
* @copyright 2011 Company
* @version 1.0.0.0
*/
提前致谢!
不,没有本地 php 函数可以从文件中提取您列出的 1.0.0.0 版本。但是,您可以编写一个:
A. 您可以逐行解析文件并使用 preg_match()
B. 你可以使用 grep 作为系统调用
$string = file_get_contents("/the/php/file.php");
preg_match("/\*\s+@version\s+([0-9.]+)/mis", $matches, $string);
var_dump($matches[1]);
您可能可以编写更有效的方法,但这可以完成工作。
这是一个使用 fgets 的测试函数,改编自Drupal Libraries API 模块:
/**
* 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;
}