doujia9204 2009-10-30 17:15
浏览 150
已采纳

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

I'm currently building a little CMS for a smaller site. Now I want to extract all words from the text_content field and store them in my word table for later analysis.

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

Currently I'm using the following code, which runs very slowly (understandably) for larger chunks of text.

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

Now, I'm not happy with this solution. I was thinking of creating a trigger in the database which would do pretty much the same thing as the php version. Is it possible to create a trigger in MySQL which would perform said actions, if so - how? Or is there a better way? Am I taking a crazy approach to this?

  • 写回答

3条回答 默认 最新

  • dongxixian7803 2009-10-30 19:19
    关注

    You could make this PHP code significantly faster by building up a single insert query and executing it rather than a separate query for every word. Otherwise, I don't think your code looks that bad.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器