Skip to content

Instantly share code, notes, and snippets.

@michimani
Last active March 2, 2018 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michimani/9fa212e432e8356eed9748fa0fe9a9e5 to your computer and use it in GitHub Desktop.
Save michimani/9fa212e432e8356eed9748fa0fe9a9e5 to your computer and use it in GitHub Desktop.
Run "cowsay -f" command randomly on PHP.
<?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