diff options
author | Fbenas <philbeansburton@gmail.com> | 2015-12-05 18:20:49 +0000 |
---|---|---|
committer | Fbenas <philbeansburton@gmail.com> | 2015-12-05 18:20:49 +0000 |
commit | 5bee1bb52dff7078a1d44c6c839a19e37c4ff248 (patch) | |
tree | 44d0f83412ad0e90df0975a7972f7b63f50ceff7 | |
parent | 7ace09e759d8bc1e45169e6d687298de8770b636 (diff) |
Add stdin for search term and allow multiple words to search for
-rw-r--r-- | search.php | 34 |
1 files changed, 29 insertions, 5 deletions
@@ -16,14 +16,27 @@ try { curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $output = json_decode(curl_exec($curl)); curl_close($curl); - - $search = "abel"; + $search = read_stdin(); + + if (empty($search)) { + throw new Exception("Please enter a search term"); + } + + $args = explode(" ", $search); // Really slow search $results = []; foreach ($output as $upload) { - if (strpos($upload->filename, $search) !== false) { + $match = false; + foreach ($args as $arg) { + if (strpos($upload->filename, $arg) === false) { + $match = false; + break; + } + $match = true; + } + if ($match) { $results[] = $config["url"] . "/" . $upload->filename; } } @@ -32,9 +45,20 @@ try { if (empty($results)) { echo "No results found\n"; } else { - var_dump($results);die(); + foreach($results as $result) { + echo $result . "\n"; + } } } catch (Exception $e) { - var_dump($e->getMessage());die(); + echo $e->getMessage() . "\n";die(); +} + +function read_stdin() +{ + $fr=fopen("php://stdin","r"); // open our file pointer to read from stdin + $input = fgets($fr,128); // read a maximum of 128 characters + $input = rtrim($input); // trim any trailing spaces. + fclose ($fr); // close the file handle + return $input; // return the text entered }
\ No newline at end of file |