【发布时间】:2010-12-11 16:34:33
【问题描述】:
我目前正在为一个较小的网站构建一个小型 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
目前我正在使用以下代码,对于较大的文本块,它运行非常缓慢(可以理解)。
// 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 sql mysql text codeigniter