William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

PHP code pacakge of ‘search files in directory’ for share

PHP code pacakge of ‘search files in directory’ for share

Support you have a directory which stores all your daily notes. How do you search the keyword? retrieve the context? There exist some software such as UltraEdit and VC Studio which are not free, or hard to use. I want a PHP script to search files in certain directory to get the context according to keyword.
However, the web seems not a very suitable solution for me, so I have to write my own version.

I wrote my version search script – The ‘search.php‘. It runs perfect, the advantages:

  • Web-based, easy use, user friendly.
  • Ajax-driven.
  • The display results is adjustable. The matching times are displayed right side of the mapping files.
  • keyword is highlight, easy to identify.
  • Display looks good. The search form uses jqtransform – a jQuery Plugin CSS render library.
  • The codes are simple and easy to integrated with other apps. Ccan be shared.
  • The package include all: the ‘search.php‘ file, the jquerytransform library, as well as jquery itself. So ALL included, no need extra pacakge. just download this package to use.
  • Any tech issues, I am here for consulting 🙂

The pacakge can be download HERE. And the core search function are list below:

function search_dir($search_dirs, $search_suffix)
{
 $keyword = $_POST['keyword'];
 $skips = array('.', '..');
 foreach($search_dirs as $dir) {
  $handle = opendir($dir);
  echo "<dl>\n";
  while($file = readdir($handle)) {
   if(in_array($file, $skips)) continue;
   elseif(is_dir($dir."/".$file)) {
    echo "sub directory<br>\n";
    continue;
  }
  elseif(preg_match("/($search_suffix)$/i", $file)) { 
   $matches = array();
   $data = file_get_contents($dir."/".$file);
   if (preg_match_all("/((\s\S*){0,3})($keyword)((\s?\S*){0,3})/i",
    $data, $matches, PREG_SET_ORDER) ) {
    $num = count($matches);
    echo '<dt><a href="'.$dir.$file.'" target="_block" 
     class="keyword">'.htmlspecialchars($file).'('.$num.")</a>  (
     <a href='javascript:void(0);'>+</a>)</dt>\n
     <span style='display:none;'>\n";
    for ($i=0; $i<count($matches); $i++) {
     if(!empty($matches[$i][3]))
      printf("<dd><em>%s</em>, <b><font color='red'>%s</font>
       </b>, <i>%s</i></dd>\n", 
       $matches[$i][1], $matches[$i][3], $matches[$i][4]);
     }
    echo "</span>\n";
   }
   flush();
  }
 }
 echo "</dl>\n";
 closedir($handle);
?>
<script language="javascript" type="text/javascript">
$("dt a[href='javascript:void(0);']").each(function(index) {
 $(this).click(function(e){
  e.preventDefault();
  $(this).parent().next('span').toggle();
  if($(this).text()=='+') $(this).text('-');
  else $(this).text('+');
  return false;
 });
});
if(! $('#show').length) {
 $show=$('<a></a>').attr({id:'show',href:'javascript:void(0);'})
   .html('<strong>Show All Details</strong>').click(function() {
$('dl').find('span').show().end().find('a[href="javascript:void(0);"]')
   .text('-');
 });
 $hide=$('<a></a>').attr({id:'hide',href:'javascript:void(0);'})
  .html('<strong>Hide All Details</strong>').click(function() {
$('dl').find('span').hide().end().find('a[href="javascript:void(0);"]')
   .text('+');
 });
 $('<div></div>').insertBefore($('#div_list')).append($show).
  append($('<span>    </span>')).append($hide);
}
</script>
<?php
 }
}
Advertisements

One response to “PHP code pacakge of ‘search files in directory’ for share

  1. 托福補習班 09/24/2011 at 6:54 pm

    I hope other people find your write-up here as practical as I have. I manage a site myself and would be happy for you or the guests on your own site to visit. Please go ahead and browse through my website like I have with yours and post a remark or two if you discover anything interesting. Many thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: