gpt4 book ai didi

php - 从SQL中的文本字段中提取单词

转载 作者:太空宇宙 更新时间:2023-11-03 10:29:50 25 4
gpt4 key购买 nike

我目前正在为一个较小的站点构建一个小型 CMS。现在我想从 text_content 字段中提取所有单词并将它们存储在我的 word 表中以供以后分析。

page( id int, 
title varchar(45),
# ... a bunch of meta fields ...
html_content text,
text_content text);

word( page_id int, # Foreign key
word varchar(100)); # I presume there are no words longer than 100 chars

目前我正在使用以下代码,它对于较大的文本 block 运行速度非常慢(可以理解)。

// Sidenote: $_POST is sanitized above scope of this code.
$_POST['text_content'] = str_replace("\t", "",
htmlspecialchars_decode(strip_tags($_POST['html_content'])));

// text is in swedish, so we add support for swedish vowels
$words = str_word_count($_POST['text_content'], 1, "åäöÅÄÖ");

// Delete all previous records of words
$this->db->delete("word", array('page_id' => $_POST['id']));

// Add current ones
foreach($words as $word)
{
if (trim($word) == "")
continue;

$this->db->query("INSERT INTO word(page_id, word) VALUES(?, ?)",
array($_POST['id'], strtolower(trim($word))));
}

现在,我对这个解决方案不满意。我正在考虑在数据库中创建一个触发器,它可以做与 php 版本几乎相同的事情。 是否可以在 MySQL 中创建一个触发器来执行上述操作,如果可以的话 - 如何实现?或者,还有更好的方法?我对此采取了疯狂的做法吗?

最佳答案

通过构建单个插入查询并执行它而不是对每个单词单独查询,您可以显着加快此 PHP 代码的速度。否则,我不认为您的代码看起来那么糟糕。

关于php - 从SQL中的文本字段中提取单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1651150/

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