2011-04-27 3 views

답변

1

아니요, 나열된 버전 1.0.0.0을 파일에서 추출하는 네이티브 PHP 함수가 없습니다. 그러나, 당신은 하나를 쓸 수 있습니다 :

A. 당신은 라인으로 파일 라인을 구문 분석 할 수 및 사용는 preg_match()를

B. 당신이 시스템 호출 여기

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

아마도 더 효율적인 방법을 쓸 수는 있지만 작업이 완료됩니다.

+1

'\/*'는 OP 예제에서 작동하지 않습니다. 내가 코멘트 블록 구문 alltogether와 일치하는 것을 피할 것입니다. '@ version'을 확인하면 충분할 것입니다. – mario

+0

사실, 정규식을 업데이트했습니다. 나는 아마도 fopen을 할 것이고, */당신이 너무 멀리 갔다면, (strstr ('@ version', $ line)) 정규 표현식을 수행한다면, fgets를 할 것이다. 더 효율적으로로드합니다. – Halcyon

0

로 그렙을 사용 할 수 있습니다 사용하는 테스트 기능입니다 fgets, 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; 
} 
관련 문제