Skip to content

Displaying SVN Revision Number as Version Number in PHP

This content will be shown before all post

In a recent project (and now probably future ones too!) I wanted to somehow display the SVN revision number at each of the individual development checkouts as well as the client testing location vs. the production environment.  As this would be somewhat public, I also wanted it to cleverly be displayed as a “Version” number, so used the following snippet.

This takes the revision number and segments the last two digits.  So, say you were at revision number 180, it would display “Version: 1.8.0”.  If you are just starting your SVN, it will display your version as 0.0.1.

<?php $svn = File('.svn/entries');
$svnrev = $svn[3];
unset($svn);
if (strlen($svnrev) > 3){
 echo substr($svnrev,0, strlen($svnrev)-3) . '.' . substr($svnrev,-3,1) . '.' . substr($svnrev,-2,1);
} else if (strlen($svnrev) == 3) {
 echo '0.' . substr($svnrev,-3,1) . '.' . substr($svnrev,-2,1);
} else {
 echo '0.0.' . substr($svnrev,-2,1);
}
?>

 

This content will be shown after all post