0

I am working on a project and part of the process is to check which version of software installed on the host server, specifically Git for this case. From the command line I can run git --version which would return something similar to:

git version 1.8.5.2 (Apple Git-48)

I can use the following code to return the value from the command.

<?php

$gitVersion = exec('git --version');

return $gitVersion;

?>

However, is there a more efficient way to have the output look like the following:

1.8.5.5

3
  • 1
    Not really, executing a shell command maybe for the best in this case. Commented Jul 21, 2014 at 1:10
  • Thank you, so is there a recommended way to strip everything except the numbers? Maybe using regex? Commented Jul 21, 2014 at 1:13
  • Since git version has a fixed length, using substrings is probably easier. Commented Jul 21, 2014 at 1:18

2 Answers 2

1
<?php
    return shell_exec("git --version | awk '{print $3}'");
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Use explode() and return the second index.

Example:

$parts = explode(' ', shell_exec('git --version'));
return $parts[2];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.