Jump to content

Search text in a Word document with PHP


Mosdn85

Recommended Posts

Hey,

 

I need some help.

 

 

I have 100 word documents. I want to search text in word documents.

 

My first step is searching text in title of word documents, That works! 

But know I want to do a more advanced search.

 

Can someone help me with that?

 

Code:

<?php
 
function ft_search_info() {
  return array(
    'name' => 'Search: Search files.',
  );
}
 
function ft_search_sidebar() {
  $sidebar[] = array(
    "id" => "search_1",
    "content" => '<div class="section">
  <h2>'.t('Search files & folders').'</h2>
  <form action="" method="post" id="searchform">
  <div>
  <input type="text" name="q" id="q" size="16" value="'.$_REQUEST['q'].'" />
  <input type="button" id="dosearch" value="'.t('Search').'" />
  </div>
  <div id="searchoptions">
  <input type="checkbox" name="type" id="type" checked="checked" /> <label for="type">'.t('Search only this folder and below').'</label>
  </div>
  <div id="searchresults"></div>
  </form>
  </div>'
  );
  return $sidebar;
}
 
 
function ft_search_ajax($act) {
  if ($act == 'search') {
    $new = array();
  $ret = "";
  $q = $_POST['q'];
  $type = $_POST['type'];
  if (!empty($q)) {
  if ($type == "true") {
  $list = _ft_search_find_files(ft_get_dir(), $q);
  } else {
  $list = _ft_search_find_files(ft_get_root(), $q);
  }
  if (is_array($list)){
  if (count($list) > 0) {
  foreach ($list as $c) {
  if (empty($c['dir'])) {
  $c['dirlink'] = "/";
  } else {
  $c['dirlink'] = $c['dir'];
  }
  if ($c['type'] == "file") {
   $link = "<a href='".ft_get_root()."{$c['dir']}/{$c['name']}' title='" .t('Show !file', array('!file' => $c['name'])). "'>{$c['shortname']}</a>";
   if (HIDEFILEPATHS == TRUE) {
     $link = ft_make_link($c['shortname'], 'method=getfile&dir='.rawurlencode($c['dir']).'&file='.$c['name'], t('Show !file', array('!file' => $c['name'])));
   }
  $ret .= "<dt>{$link}</dt><dd>".ft_make_link($c['dirlink'], "dir=".rawurlencode($c['dir'])."&highlight=".rawurlencode($c['name'])."&q=".rawurlencode($q), t("Highlight file in directory"))."</dd>";
  } else {
  $ret .= "<dt class='dir'>".ft_make_link($c['shortname'], "dir=".rawurlencode("{$c['dir']}/{$c['name']}")."&q={$q}", t("Show files in !folder", array('!folder' => $c['name'])))."</dt><dd>".ft_make_link($c['dirlink'], "dir=".rawurlencode($c['dir'])."&highlight=".rawurlencode($c['name'])."&q=".rawurlencode($q), t("Highlight file in directory"))."</dd>";
  }
  }
  return $ret;
  } else {
  return "<dt class='error'>".t('No files found').".</dt>";
  }
  } else {
  return "<dt class='error'>".t('Error.')."</dt>";
  }
  } else {
  return "<dt class='error'>".t('Enter a search string.')."</dt>";
  }
  }
}
 
function ft_search_add_js_call() {
  $return = '';
  $return .= "$('#searchform').ft_search({\r\n";
  if (!empty($_REQUEST['dir'])) {
    $return .= "\tdirectory: '{$_REQUEST['dir']}',\r\n";
  } else {
    $return .= "\tdirectory: '',\r\n";
  }
  $return .= "\tformpost: '".ft_get_self()."',\r\n";
  $return .= "\theader: '".t('Results')."',\r\n";
  $return .= "\tloading: '".t('Fetching results…')."'\r\n";
  $return .= '});';
  return $return;
}
 
function _ft_search_find_files($dir, $q){
$output = array();
if (ft_check_dir($dir) && $dirlink = @opendir($dir)) {
while(($file = readdir($dirlink)) !== false){
if($file != "." && $file != ".." && ((ft_check_file($file) && ft_check_filetype($file)) || (is_dir($dir."/".$file) && ft_check_dir($file)))){
$path = $dir.'/'.$file;
// Check if filename/directory name is a match.
if(stristr($file, $q)) {
$new['name'] = $file;
$new['shortname'] = ft_get_nice_filename($file, 20);
$new['dir'] = substr($dir, strlen(ft_get_root()));
if (is_dir($path)) {
            if (ft_check_dir($path)) {
  $new['type'] = "dir";    
    $output[] = $new;
            }
} else {
 $new['type'] = "file";
  $output[] = $new;
}
}
// Check subdirs for matches.
if(is_dir($path)) {
$dirres = _ft_search_find_files($path, $q);
if (is_array($dirres) && count($dirres) > 0) {
$output = array_merge($dirres, $output);
unset($dirres);
}
}
}
}
sort($output);
closedir($dirlink);
return $output;
} else {
return FALSE;
}
}
 

 

Regards,

Mo

Link to comment
Share on other sites

Word documents are xml that has been compressed. You need to uncompress it and search the pieces you want. I've never done that but have gone the other way, using tinybutstrong and OpenTBS. I think it has what you need built in, but I don't have time to make sure now. You can look yourself at http://www.tinybutstrong.com/opentbs.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.