PHP & MYSQL-如何搜索单词并将它们像句子一样进行翻译?

我正在创建用户可以搜索的位置"How are you"并将翻译输出到"Como son tu"


My database looks like this


English         Translation

How             Como

Are             Son

You             Tu

我的代码:


if(($_POST['translateKey'])){

  $translateWord = mysql_fix_string($connection, $_POST['translateWord']);

  if (!empty($translateWord)) {

    $query = "SELECT * FROM $username";

    $result = $connection->query($query);


    if (!$result) {

        if (!$result) die("Couldn't query data: " . $connection->error);

    }

    $rows = $result->num_rows;

    $isTranslation = false;

    for ($i = 0; $i < $rows; ++$i) {

        $result->data_seek($i);


        $row = $result->fetch_array(MYSQLI_ASSOC);

        //using strpos to find the first occurrence of a substring in a string

        $pos = strpos($row['English'],$translateWord);


        if ($pos !== false) {

            echo $row['Translation'] . "</br>";

            $isTranslation = true;

            break;

         } 

    }

    if (!$isTranslation)

                    echo "No translation can be found!</br>";

            }

    }

这是我的代码,但它只能搜索单个字符"How" -> "Como",如果我尝试搜索多个字符串,它会给我"No translation can be found!"


有没有一种方法可以搜索多个字符串并将字符作为句子输出?喜欢搜索"How are you"并获取"Come son tu"


我正在为学习目的创建一个蹩脚的翻译器哈哈。


胡子哥哥
浏览 115回答 1
1回答

九州编程

我整理了一些 PHP,它们将尝试处理整个输入字符串(例如How are you)。它将输入字符串转换为单词数组,然后使用该数组为IN表达式 ( How are you=> 'how','are','you') 生成适当的字符串,然后将其添加到查询中。我已将查询稍微修改为小写所有内容(以便how可以匹配How或HOW或...)。在演示中,我模拟了一个结果集,您可以使用$result->fetch_all(). 代码循环遍历单词列表,尝试匹配结果数组中的每个单词,如果找到,则尝试匹配大小写(它处理大写、大写和小写)。结果集中未找到的单词将通过未翻译的方式传递。$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&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM $username&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHERE LOWER(English) IN ($list)";// query table// $result = $connection->query($query);// $rows = $result->fetch_all();// simulated result$rows = array(array('English' => 'how', 'Translation' => 'como'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array('English' => 'are', 'Translation' => 'son'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array('English' => 'you', 'Translation' => 'tu'));$translation = array();foreach ($words as $word) {&nbsp; &nbsp; if (($key = array_search(strtolower($word), array_column($rows, 'English'))) !== false) {&nbsp; &nbsp; &nbsp; &nbsp; // match case of result&nbsp; &nbsp; &nbsp; &nbsp; if (strtoupper($word) == $word) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $translation[] = strtoupper($rows[$key]['Translation']);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; elseif (ucfirst($word) == $word) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $translation[] = ucfirst($rows[$key]['Translation']);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $translation[] = $rows[$key]['Translation'];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; // no match, leave alone&nbsp; &nbsp; &nbsp; &nbsp; $translation[] = $word;&nbsp; &nbsp; }}echo $translateWord . " => " . implode(' ', $translation);输出:How are you => Como son tu
打开App,查看更多内容
随时随地看视频慕课网APP