duanpu5048 2011-05-07 11:54
浏览 70
已采纳

使用PHP解析文本文件并保持每个单词的偏移量

I'm trying to parse a text file word by word and I need to be able to to store the offset of each word from the beginning of the file so that I can then find the exact instance of the word in the text file. Now I'm dealing with some very large text files so I was wondering what is the most efficient way of doing this?

EDIT: Some more details.

I will have two tables in a database. Lets call them WORDS which stores the words and REFERENCES which a reference for each word to a file.

The REFERENCES table will be something like this:

id INT PRIMARY
file_id INT /* ID of the text file */
offset INT /* Offset from the start of the file to get to the start of the word */

Then each record of WORDS has a referenced_id which related to a record in REFERENCES.

This is a simplified example. In reality I'll have another table that relates each record from WORDS to one or more records in REFERENCES as there may be many instances of each word in many files.

The purpose of all this is to be able to show exactly where the instances of the word have been found when searching. So when I find the word I can get it's reference and then take an excerpt from the text file.

  • 写回答

1条回答 默认 最新

  • douhuang1973 2011-05-07 12:33
    关注

    most effective bu time or by memory used?

    1. By time - load all file in memory and parse it. And then popup each word, storing it (word) length

    $page = file_get_contents( $file ); $arr_w = explode( " ", $page );

    $arr_c = array(); count of words $arr_o = array(); offset of words :)

    $c = 0; foreach ($arr_w as $w) { $c++; str_replace( $page, $w, "", 1); $arr_c[$w]++; $arr_o[$c] = strlen($w) + 1; }

    This is just idea ...

    1. By memory usage, - your will need continuously read file and analise. It is much differs from this example ...
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题