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;
}
}
}