0

is there a way (a php function) to get the version 1.0.0.0 from a file?

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

thanks in advance!

0

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0
$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.

2 Comments

The \/* will not work on OPs example. I would avoid trying to match the comment block syntax alltogether. Checking for @version would suffice I guess.
true, updated the regex. I would probably just do an fopen and do a couple of fgets, if you see */ you've gone too far, if(strstr('@version', $line)) do the regex. Loads more efficient.
0

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; } 

Comments