Skip to content

Displaying SVN Revision Number as Version Number in PHP

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

 

Find this useful? Take just a moment and give a $1. Thanks!
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)