在没有数据库的PHP中全文搜索

时间:2014-01-27 23:37:20

标签: php full-text-search search-engine

我有一个用PHP编写的非常小的网页(大约5页+博客条目)。所有页面都位于服务器端的php文件中(不使用数据库)。到目前为止,我设法在我的“博客条目”中搜索 - 因为这些只是带有HTML标记的纯文本文件(我剥离标签并执行搜索操作):

$file_name=array();
$search_string="";
if(isSet($_GET["query"])){
    $search_string=$_GET["query"];
}
$search_result="";
$files="";
$phpfilename="";
$i=0;   
if (!$search_string){
    echo 'No query entered<br />';
}else{
    if ($handle = opendir('content/')) { 
        while (false !== ($file = readdir($handle))){
            if(strrchr($file, '.') === ".txt"){
                $filename[]= $file;
            }
        } 
        closedir($handle); 
    }
    foreach($filename as $value){
        $files="content/$value";
        $fp = strip_tags(file_get_contents($files));
        if(stripos($fp, $search_string)) {
            $search_result.=preg_replace('/<[^>]*>[^<]*<[^>]*>/', '', substr($fp,0,255)); // append a preview to search results
        }
        if($search_result!=""){
            echo $search_result;
        }else{
            echo "No Results<br />";
        }
    }
}

当然这只是因为文件是纯文本。但我也有真正的'php'文件页面,并希望对它们执行搜索操作。但我当然不想在'PHP代码'里面搜索。我想,我需要浏览器从网络服务器获取的预先准备好的文件 - 我想到file_get_contents()使用http请求到我的所有页面(好吧,'只是'约5页但仍然)..

我在这里读过这样做被认为是不好的做法,感觉我采取了错误的做法。

任何想法&amp;建议将受到高度赞赏。

编辑:我希望能够在

中搜索的常规页面的示例

的index.php

<?php ob_start(); require_once("./include/common.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo $lang['WEBSITE_TITLE']; ?></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="keywords" content="keyword, keyword, keyword" />
<link href="css/main.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="page">
<!-- Header Area -->
<?php include("./include/header.php"); ?>
<?php include("./include/banner.php"); ?>
<div id="content">

<?php

    $page = '';
    if(isSet($_GET["page"])){
        $page=$_GET["page"];
    }
    switch($page){
        case 'category_1':
            include("./include/category_1.php");
            break;
        case 'about':
            include("./include/category_2.php");
            break;
        case 'contact':
            include("./include/contact.php");
            break;
        default:
            include("./include/home.php");  
    }
?>
<!-- /content --></div> 

<!-- /page --></div>
<br />
<br /><br /><br />

<!-- Footer Area -->
<?php include("./include/footer.php"); ob_end_flush(); ?>

</body>
</html> 

/include/category_1.php

<?php echo '<h2>'.$lang['NAVI_CAT_1'].'</h2>'; ?>

<div id="entry">
<br/>
<?php echo $lang['CAT_1_TEXT']; ?>
</div>

语言文件

<?php
$lang = array();
$lang['NAVI_CAT_1'] = 'Category 1';
$lang['CAT_1_TEXT'] = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim.';

?>

2 个答案:

答案 0 :(得分:4)

为什么不包含在缓冲区中然后搜索缓冲区的内容?

ob_start();
include ('index.php');
$contents = ob_get_clean();
//the $contents now includes whatever the php file outputs

我实际上在生产代码中使用此方法来处理各种事情,但主要是在用户发送之前预览网站生成的电子邮件。好的是,你可以在所有文件上使用它,而不仅仅是php文件。

答案 1 :(得分:0)

设计失败了。 考虑不使用普通的混合html边。尝试使用xml文件或wathever。

替代方案是爬行自己的一方。看看http://symfony.com/doc/current/components/dom_crawler.html