Skip to content

Instantly share code, notes, and snippets.

@michimani

michimani/rand_cowsay.php

Last active Mar 2, 2018
Embed
What would you like to do?
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