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 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写