dongyu1614 2013-06-30 06:10
浏览 49
已采纳

php | anagram求解器通过woldcard查找不在原始搜索中的字母

I have a query as such, what I am trying to do is when a user searches my anagram solver they may input a wildcard as a ? in the search field.

<input type="text" name="l">

Lets say the user types in "tes?"

My query will find every word it can make with those letters including 1 wildcard as well

SELECT `word`, 0+IF(`a` > 0, `a` - 0, 0)+IF(`b` > 0, `b` - 0, 0)+IF(`c` > 0, `c` - 0, 0)+IF(`d` > 0, `d` - 0, 0)+IF(`e` > 1, `e` - 1, 0)+IF(`f` > 0, `f` - 0, 0)+IF(`g` > 0, `g` - 0, 0)+IF(`h` > 0, `h` - 0, 0)+IF(`i` > 0, `i` - 0, 0)+IF(`j` > 0, `j` - 0, 0)+IF(`k` > 0, `k` - 0, 0)+IF(`l` > 0, `l` - 0, 0)+IF(`m` > 0, `m` - 0, 0)+IF(`n` > 0, `n` - 0, 0)+IF(`o` > 0, `o` - 0, 0)+IF(`p` > 0, `p` - 0, 0)+IF(`q` > 0, `q` - 0, 0)+IF(`r` > 0, `r` - 0, 0)+IF(`s` > 1, `s` - 1, 0)+IF(`t` > 1, `t` - 1, 0)+IF(`u` > 0, `u` - 0, 0)+IF(`v` > 0, `v` - 0, 0)+IF(`w` > 0, `w` - 0, 0)+IF(`x` > 0, `x` - 0, 0)+IF(`y` > 0, `y` - 0, 0)+IF(`z` > 0, `z` - 0, 0) AS difference FROM `TWL06` WHERE LENGTH(`word`) <= 4 HAVING difference <= 1 ORDER BY LENGTH(`word`) DESC

My query works fine. What I want to do is based on the words it find I want it to change the color of the letter in the word it finds to blue to notify the user that this is the wildcard.

So if the user types "tes?" on of the results would be "TEST" at that point I want to be able to determine how many times the letter is used compared the amount of times it is inputed in the original search and then change the color to blue. So it would look something like this

TES<span style="color:#0000FF">T</span>

I am trying to use a foreach loop to determine this with a preg_match but I cant seem to get it to line up correctly. Any help would be appreciated. Here is an example of my code with the original search term associtated with $l as "TES?"

SELECT `word`, 0+IF(`a` > 0, `a` - 0, 0)+IF(`b` > 0, `b` - 0, 0)+IF(`c` > 0, `c` - 0, 0)+IF(`d` > 0, `d` - 0, 0)+IF(`e` > 1, `e` - 1, 0)+IF(`f` > 0, `f` - 0, 0)+IF(`g` > 0, `g` - 0, 0)+IF(`h` > 0, `h` - 0, 0)+IF(`i` > 0, `i` - 0, 0)+IF(`j` > 0, `j` - 0, 0)+IF(`k` > 0, `k` - 0, 0)+IF(`l` > 0, `l` - 0, 0)+IF(`m` > 0, `m` - 0, 0)+IF(`n` > 0, `n` - 0, 0)+IF(`o` > 0, `o` - 0, 0)+IF(`p` > 0, `p` - 0, 0)+IF(`q` > 0, `q` - 0, 0)+IF(`r` > 0, `r` - 0, 0)+IF(`s` > 1, `s` - 1, 0)+IF(`t` > 1, `t` - 1, 0)+IF(`u` > 0, `u` - 0, 0)+IF(`v` > 0, `v` - 0, 0)+IF(`w` > 0, `w` - 0, 0)+IF(`x` > 0, `x` - 0, 0)+IF(`y` > 0, `y` - 0, 0)+IF(`z` > 0, `z` - 0, 0) AS difference FROM `TWL06` WHERE LENGTH(`word`) <= 4 HAVING difference <= 1 ORDER BY LENGTH(`word`) DESC
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
    $getvalues = str_split($row['word']);
    foreach($getvalues as $letter) {
        if (!preg_match('/'.$letter.'/',$l)) {
            $buildword = '<span style="color:#0000FF">'.$letter.'</span>';
        } else {
            $buildword = $letter;
        }
    }
}
  • 写回答

1条回答 默认 最新

  • duan_88598 2013-06-30 06:50
    关注

    One problem with your strategy was that the word "test" has two ts, so I decided to assign the input value to $temp (just in case you need to use $l somewhere else) and each time a letter is found subtract it. If the user entered "tes" it would remove those letters one by one until there is nothing left, and the final t in "test" gets displayed in blue.

    while($row = mysql_fetch_array($result)) {
        $getvalues = str_split($row['word']);
        $temp = $l;
        $buildword = '';
        foreach($getvalues as $letter) {
            $pos = strpos($temp, $letter);
            if ($pos === FALSE) {
                $buildword .= '<span style="color:#0000FF">'.$letter.'</span>';
            } else {
                if ($pos == 0){
                    $temp = substr($temp, 1);
                } else {
                    $first = substr($temp, 0, $pos);
                    $temp = $first.substr($temp, $pos+1);
                }
                $buildword .= $letter;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?