2014-02-24 2 views
0

나는 5000 파일에 대해 온라인으로 단어 파일을 가지고 있는데, 어떤 키워드에 대해서도 모든 파일에서 검색해야합니다. 예를 들어, "Human Resource"는 입니다. 거대한 단어 파일을 검색하는 가장 좋은 방법

그래서 나는 워드 파일을 읽을 기능을 만들었지 만 내 문제 내가
예제 코드 서버의 메모리를 죽일 작업을 처리 같아요

<?php 
function doc_to_text($input_file){ //for doc files 
    $file_handle = @fopen($input_file, "r"); //open the file 
    $stream_text = @fread($file_handle, filesize($input_file)); 
    $stream_line = explode(chr(0x0D),$stream_text); 
    $output_text = ""; 
    foreach($stream_line as $single_line){ 
     $line_pos = strpos($single_line, chr(0x00)); 
     if(($line_pos !== FALSE) || (strlen($single_line)==0)){ 
      $output_text .= ""; 
     }else{ 
      $output_text .= $single_line." "; 
     } 
    } 
    $output_text = preg_replace("/[^a-zA-Z0-9\s\,\.\-\n\r\[email protected]\/\_\(\)]/", "", $output_text); 
    return $output_text; 
} 


function docx_to_text($input_file){ //for docx files 
    $xml_filename = "word/document.xml"; //content file name 
    $zip_handle = new ZipArchive; 
    $output_text = ""; 
    if(true === $zip_handle->open($input_file)){ 
     if(($xml_index = $zip_handle->locateName($xml_filename)) !== false){ 
      $xml_datas = $zip_handle->getFromIndex($xml_index); 
      $xml_handle = DOMDocument::loadXML($xml_datas, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING); 
      $output_text = strip_tags($xml_handle->saveXML()); 
     }else{ 
      $output_text .=""; 
     } 
     $zip_handle->close(); 
    }else{ 
    $output_text .=""; 
    } 
    return $output_text; 
} 





?> 

은 그럼 루프를 만들고 stristr하여 키워드를 모든 파일을 검사합니다() 함수를 호출하고 stristr()이 true를 반환하면 스크립트는 파일 이름을 인쇄합니다.

다른 해결책이 있습니까?

참조 : stristr()

+2

네, 검색 색인을 조기에 만들어서 검색 할 수 있습니다. – zerkms

+0

awk, sed를 사용해 보셨습니까? – ziollek

답변

1

당신은 각 단어를 매핑 (또는 문서에 심지어 문구를 원한다면 할 수있다) inverse index라는 구조를 만들어야합니다. 위키 페이지는 프로세스를 멋지게 문서화하고 있으며 이는 매우 간단합니다.

데이터베이스에이 구조를 저장할 수 있습니다 (전처리 단계에서 한 번만 수행됨). 나중에 새 Doc 또는 Docx 파일을 추가 할 때이 구조가 변경 될 수 있습니다.

사용자가 단어를 삽입하면 파일이 아닌 데이터베이스에서 검색되므로 색인이 빠르게 사용되고 유용합니다.