gpt4 book ai didi

php - 在数据库中搜索单词数组

转载 作者:行者123 更新时间:2023-11-29 12:53:45 24 4
gpt4 key购买 nike

我有文字:

$a = I wanna eat apple , and banana .

我想获取该句子的每个单词和标点符号:

$b = explode(' ', strtolower(trim($a)));

explode的结果是数组。我在数据库上有一个包含字段的单词表:

id, word and typewords
all in lowercase. but for punctuation there are no exist in db.I wanna search every words in db to take the type of words, so the final result that i want to get is : words/typeofwords =
I/n wanna/v eat/v apple/n ,/, and/p banana/n ./.
here's the code :

function getWord ($word){
$i = 0 ;
$query = mysql_query("SELECT typewords FROM words WHERE word = '$word' ");
while ($row = mysql_fetch_array($query)) {
$word[$i] = $row['typewords'];
$i++;
}
return $word;
}

echo $b.'/'.getWord($b);

但是不行,请帮帮我,谢谢!

最佳答案

试试这个:

function getWord($words){
$association = array();
foreach($words as $word)
{
$query = mysql_query("SELECT typewords FROM words WHERE word = '$word' ");
if($row = mysql_fetch_array($query))
$association[$word] = $row['typewords'];
elseif(preg_match('/[\.\,\:\;\?\!]/',$word)==1)
$association[$word] = $word;
}
return $association;
}

$typewords = getWord($b);
foreach($b as $w)
echo $w.'/'.$typewords[$w];

关于php - 在数据库中搜索单词数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24396242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com