ファイルからバージョン 1.0.0.0 を取得する方法 (php 関数) はありますか?
/**
* @author softplaxa
* @copyright 2011 Company
* @version 1.0.0.0
*/
前もって感謝します!
いいえ、リストしたバージョン 1.0.0.0 をファイルから抽出するネイティブ php 関数はありません。ただし、次のように書くことができます。
A. ファイルを 1 行ずつ解析し、preg_match() を使用できます。
B. grep をシステムコールとして使用できます
Drupal LibrariesAPIモジュールから採用されたfgetsを使用するテスト済みの関数は次のとおりです。
/**
* 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;
}
$string = file_get_contents("/the/php/file.php");
preg_match("/\*\s+@version\s+([0-9.]+)/mis", $matches, $string);
var_dump($matches[1]);
おそらくもっと効率的なものを書くことができますが、これで仕事は完了です。