From 5bee1bb52dff7078a1d44c6c839a19e37c4ff248 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Sat, 5 Dec 2015 18:20:49 +0000 Subject: Add stdin for search term and allow multiple words to search for --- search.php | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/search.php b/search.php index 8d027a0..f2acce8 100644 --- a/search.php +++ b/search.php @@ -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 -- cgit v1.2.3