用 php & 替换文本中的关键字mysql [英] Replacing keywords in text with php & mysql
问题描述
我有一个新闻网站,其中包含超过 100 万条新闻的档案.我创建了一个包含大约 3000 个条目的词定义数据库,由词定义对组成.
I have a news site containing an archive with more than 1 million news. I created a word definitions database with about 3000 entries, consisting of word-definition pairs.
我想做的是在新闻中每次出现这些词的旁边添加一个定义.我无法进行静态更改,因为我可以每天添加一个新关键字,因此我可以将其设为实时或缓存.
What I want to do is adding a definition next to every occurence of these words in the news. I cant make a static change as I can add a new keyword everyday, so i can make it realtime or cached.
问题是,str_replace
或 preg_replace
在文本中搜索 3000 个关键字并替换它们会非常慢.
The question is, a str_replace
or a preg_replace
would be very slow for searching 3 thousand keywords in a text and replacing them.
有什么快速的替代方法吗?
Are there any fast alternatives?
推荐答案
str_replace 对你不起作用(除非你想让superlative"中的perl"作为关键字),你需要一些考虑词边界的东西(例如用 \b
替换 preg_replace).当然,你不能一次preg_replace所有3000个关键字,但一个文档几乎不可能包含所有这些,因此我建议对所有文档进行预索引,例如通过维护一个索引表doc_id->word_id.服务特定文档时,查询索引,只替换文档实际包含的关键字(大概不超过100个).
str_replace won't work for you (unless you want "perl" in "superlative" to be a keyword), you need something that takes word boundaries into account (e.g. preg_replace with \b
). Of course, you cannot preg_replace all 3000 keywords at once, but one single document can hardly contain them all, therefore I'd suggest pre-indexing all documents, for example, by maintaining an index table doc_id->word_id. When serving a specific document, query the index and only replace keywords that the document actually contains (presumably no more than 100).
另一方面,如果文档很短,维护索引表可能不值得麻烦.您可以简单地动态进行预索引,例如使用 strpos
:
On the other side, if documents are short, maintaining the index table might not be worth the trouble. You can simply do pre-indexing on the fly, e.g. with strpos
:
$kw = array();
foreach($all_keywords as $k) if(strpos($text, $k)) $kw[] = $k;
// $kw contains only words that actually occur in the text
// (and perhaps some more, but that doesn't matter)
preg_replace_callback('/\b(' . implode('|', $kw) . ')\b/', 'insert_keyword', $text)
这篇关于用 php & 替换文本中的关键字mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!