Last active
March 2, 2018 00:29
-
-
Save michimani/9fa212e432e8356eed9748fa0fe9a9e5 to your computer and use it in GitHub Desktop.
Run "cowsay -f" command randomly on PHP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (count($argv) < 2) | |
{ | |
echo 'parameter not enough.'."\n"; | |
return false; | |
} | |
exec('which cowsay 2>&1', $cmd_t); | |
if (!isset($cmd_t[0]) || $cmd_t[0] == '') | |
{ | |
echo 'cowsay not found.'."\n"; | |
return false; | |
} | |
$argv = array_reverse($argv); | |
$txt = $argv[0]; | |
$cmd = $cmd_t[0]; | |
if (in_array('-t', $argv)) | |
{ | |
$cmd = str_replace('cowsay', 'cowthink', $cmd); | |
} | |
$c = []; | |
exec(sprintf('%s -l 2>&1', $cmd), $cl); | |
foreach ($cl as $k => $l) | |
{ | |
if ($k === 0) | |
{ | |
continue; | |
} | |
$c = array_merge($c, explode(' ', $l)); | |
} | |
$idx = rand(1,count($c)) - 1; | |
exec(sprintf('%s -f %s "%s"', $cmd, $c[$idx], $txt), $res); | |
foreach ($res as $r) | |
{ | |
echo $r."\n"; | |
} | |
echo "\n\nby {$c[$idx]}\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment