added special search

This commit is contained in:
hnhx 2022-02-18 10:53:47 +01:00
parent 4b27489595
commit a91667b8dc
4 changed files with 86 additions and 3 deletions

View file

@ -6,6 +6,80 @@
return $base_url;
}
function special_search($query)
{
$query_lower = strtolower($query);
// Check for currency convesion
if (str_contains($query_lower, "to"))
convert_currency($query);
// Check for definition
else if (str_contains($query_lower, "mean"))
define_word($query);
}
function convert_currency($query)
{
$split_query = explode(" ", $query);
if (count($split_query) >= 4)
{
$amount_to_convert = floatval($split_query[0]);
if ($amount_to_convert != 0)
{
$base_currency = strtoupper($split_query[1]);
$currency_to_convert = strtoupper($split_query[3]);
$ch = curl_init("https://cdn.moneyconvert.net/api/latest.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$json_response = json_decode($response, true);
$rates = $json_response["rates"];
if (array_key_exists($base_currency, $rates) && array_key_exists($currency_to_convert, $rates))
{
$base_currency_response = $rates[$base_currency];
$currency_to_convert_response = $rates[$currency_to_convert];
$conversion_result = ($currency_to_convert_response / $base_currency_response) * $amount_to_convert;
echo "<p id=\"special-result\">";
echo "$amount_to_convert $base_currency = $conversion_result $currency_to_convert";
echo "</p>";
}
}
}
}
function define_word($query)
{
$split_query = explode(" ", $query);
if (count($split_query) >= 2)
{
$reversed_split_q = array_reverse($split_query);
$word_to_define = $reversed_split_q[1];
$ch = curl_init("https://api.dictionaryapi.dev/api/v2/entries/en/$word_to_define");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$json_response = json_decode($response, true);
if (!array_key_exists("title", $json_response))
{
$definition = $json_response[0]["meanings"][0]["definitions"][0]["definition"];
echo "<p id=\"special-result\">";
echo "$word_to_define meaning<br/>";
echo "<br/>" . $definition . "<br/>";
echo "</p>";
}
}
}
function fetch_results($query, $page, $get_images=false)
{
require("config.php");

View file

@ -29,7 +29,7 @@
$query = $_REQUEST["q"];
echo "value=\"$query\"";
$_SESSION["q"] = $query;
$_SESSION["q"] = trim($query);
$_SESSION["p"] = $_REQUEST["p"];
$_SESSION["type"] = $_REQUEST["type"];
}

View file

@ -29,9 +29,10 @@
$end_time = number_format(microtime(true) - $start_time, 2, '.', '');
echo "<p id=\"time\">Fetched the results in $end_time seconds</p>";
if ($_SESSION["type"] != "img")
{
special_search($query);
foreach($results as $result)
{
@ -78,6 +79,8 @@
}
require "session_destroy.php";
?>
</body>
</html>

View file

@ -99,7 +99,7 @@ img { padding:15px; }
.small-search-container { margin:2%; }
.result-container, #time {
.result-container, #time, #special-result {
margin-left:10%;
}
@ -122,6 +122,12 @@ img { padding:15px; }
margin-top:6px;
}
#special-result {
padding:10px;
border: 1px solid #bdc1c6;
width:500px;
}
.page-container {
margin-bottom:70px;
margin-left:15%;