单词查找器正则表达式、PHP 和mysql

发布于 2024-11-09 13:00:27 字数 864 浏览 0 评论 0原文

我有一个正在尝试针对单词数据库运行的查询。查询必须返回一个包含另一个单词和给定字母的单词。它是用 PHP 和 mySQL 编写的。

例如:

Word Given: Cruel
Letters Given: abcdty

在数据库中,我需要根据给定的字母和给定的单词找到“Cruelty”这个单词。它需要双向发挥作用。因此,如果我的字母为“atni”,那么如果数据库中存在“Anticruel”,就会出现“Anticruel”。

我已经工作了一半,但给出的结果不正确:

SELECT word
FROM words
WHERE LOCATE(  "cruel", word ) >0
AND word !=  "cruel"
AND word
REGEXP  '[ybilteh]'

此查询的结果集:

"anticruelty"
"crueler"
"cruelest"
"crueller"
"cruellest"
"cruelly"
"cruelness"
"cruelnesses"
"cruelties"
"cruelty"

更新!!!

感谢本杰明·莫雷尔(Benjamin Morel),我们离这个目标越来越近了。

此查询:

SELECT word
FROM words
WHERE LOCATE(  "t", word ) >0
AND word !=  "t"
AND word
REGEXP  '^[ybilteh]*t[ybilteh]*$'
LIMIT 0 , 30

正确查找单词。但也包括带有双字母的单词。如“甜菜”。当只有 1 个“e”可用时。

I have a query that I'm trying to run against a database of words. The query must return a word that contains another word, and letters given. It is in PHP and mySQL.

For example:

Word Given: Cruel
Letters Given: abcdty

In the database, I need to find the word "Cruelty" based on the letters given, and the word given. It needs to works both ways. So if I had "atni" for letters, "Anticruel" would appear if it existed in the database.

I have it half working but the result given is not correct:

SELECT word
FROM words
WHERE LOCATE(  "cruel", word ) >0
AND word !=  "cruel"
AND word
REGEXP  '[ybilteh]'

The result set from this query:

"anticruelty"
"crueler"
"cruelest"
"crueller"
"cruellest"
"cruelly"
"cruelness"
"cruelnesses"
"cruelties"
"cruelty"

Update!!!

Thanks to Benjamin Morel, this is getting much closer.

This query:

SELECT word
FROM words
WHERE LOCATE(  "t", word ) >0
AND word !=  "t"
AND word
REGEXP  '^[ybilteh]*t[ybilteh]*

Finds words correctly. But also includes words with double letters. Such as "Beet". When only 1 "e" is available.

LIMIT 0 , 30

Finds words correctly. But also includes words with double letters. Such as "Beet". When only 1 "e" is available.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘曦 2024-11-16 13:00:27

试试这个:

SELECT word
FROM words
WHERE word REGEXP '^[ybilteh]*cruel[ybilteh]*

更新:让我们用 PHP 来优化,这个怎么样?

$word = 'cruel';
$letters = 'ybilteh';

$items = array("anticruelty", "crueler", "cruelest",
    "crueller", "cruellest", "cruelly", "cruelness",
    "cruelnesses", "cruelties", "cruelty");

$letters = str_split($letters);
foreach ($items as $item) {
    $list = $letters;
    // remove the original word (once)
    $thisItem = preg_replace("/$word/", '', $item, 1); 
    for ($i=0; $i<strlen($thisItem); $i++) {
        $index = array_search($thisItem[$i], $list);
        if ($index === false) {
            continue 2; // letter not available
        }
        unset($list[$index]); // remove the letter from the list
    }
    echo "$item\n"; // passed!
}

返回:残酷地,残酷地

你可能会找到更好/更简单的方法,但这应该可以解决问题!

AND word != 'cruel';

更新:让我们用 PHP 来优化,这个怎么样?

返回:残酷地,残酷地

你可能会找到更好/更简单的方法,但这应该可以解决问题!

Try this one:

SELECT word
FROM words
WHERE word REGEXP '^[ybilteh]*cruel[ybilteh]*

UPDATE: let's go refining with PHP, what about this?

$word = 'cruel';
$letters = 'ybilteh';

$items = array("anticruelty", "crueler", "cruelest",
    "crueller", "cruellest", "cruelly", "cruelness",
    "cruelnesses", "cruelties", "cruelty");

$letters = str_split($letters);
foreach ($items as $item) {
    $list = $letters;
    // remove the original word (once)
    $thisItem = preg_replace("/$word/", '', $item, 1); 
    for ($i=0; $i<strlen($thisItem); $i++) {
        $index = array_search($thisItem[$i], $list);
        if ($index === false) {
            continue 2; // letter not available
        }
        unset($list[$index]); // remove the letter from the list
    }
    echo "$item\n"; // passed!
}

Returns: cruelly, cruelty

You might probably find a better/simpler approach, but that should do the trick!

AND word != 'cruel';

UPDATE: let's go refining with PHP, what about this?

Returns: cruelly, cruelty

You might probably find a better/simpler approach, but that should do the trick!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文