<?php
$username = 'test';
$translateWord = 'How are you';
$words = explode(' ', $translateWord);
$list = implode(',', array_map(function ($word) { return "'" . strtolower(trim($word)) . "'"; }, $words));
$query = "SELECT LOWER(English) AS English, LOWER(Translation) AS Translation
FROM $username
WHERE LOWER(English) IN ($list)";
// query table
// $result = $connection->query($query);
// $rows = $result->fetch_all();
// simulated result
$rows = array(array('English' => 'how', 'Translation' => 'como'),
array('English' => 'are', 'Translation' => 'son'),
array('English' => 'you', 'Translation' => 'tu'));
$translation = array();
foreach ($words as $word) {
if (($key = array_search(strtolower($word), array_column($rows, 'English'))) !== false) {
// match case of result
if (strtoupper($word) == $word) {
$translation[] = strtoupper($rows[$key]['Translation']);
}
elseif (ucfirst($word) == $word) {
$translation[] = ucfirst($rows[$key]['Translation']);
}
else {
$translation[] = $rows[$key]['Translation'];
}
}
else {
// no match, leave alone
$translation[] = $word;
}
}
echo $translateWord . " => " . implode(' ', $translation);
- Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- How are you => Como son tu
preferences:
146.54 ms | 407 KiB | 5 Q