PHP code pacakge of 'search files in directory' for share
Blogs20112011-09-24
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("/((sS*){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);
?>
$("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=$('').attr({id:'show',href:'javascript:void(0);'})
.html('Show All Details').click(function() {
$('dl').find('span').show().end().find('a[href="javascript:void(0);"]')
.text('-');
});
$hide=$('').attr({id:'hide',href:'javascript:void(0);'})
.html('Hide All Details').click(function() {
$('dl').find('span').hide().end().find('a[href="javascript:void(0);"]')
.text('+');
});
$('').insertBefore($('#div_list')).append($show).
append($(' ')).append($hide);
}
<?php
}
}