2014-01-27 35 views
1

我有一個用php編寫的非常小的網頁(大約5頁+博客文章)。所有頁面都位於服務器端的php文件中(不使用數據庫)。到目前爲止,我管理我的「博客文章」裏面搜索 - 因爲這是用HTML標記只是普通TEXTFILES(我剝執行搜索操作的標籤&):全文搜索在沒有數據庫的php中

$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頁,但仍然)...

我在這裏讀過,認爲這樣做是不好的做法,感覺就像我採取了錯誤的做法。

任何想法&建議將不勝感激。

編輯:一種是我希望能夠在

的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.'; 

?> 
+0

爲什麼不簡單忽略之間的所有內容? – Jivan

+0

如果我們正在談論5頁,那麼只需在瀏覽器中打開它們,將源代碼複製到txt文件,然後將它們上傳到服務器,然後在搜索中使用它們。 – Steve

+1

@ user574632你剛發明了一個http請求中的file_get_contents()...手工製作! ;) – Jivan

回答

3

爲什麼不包含到緩衝區中,然後搜索緩衝區的內容?

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

其實我在生產中使用的代碼這種方法對各種事情,但主要是預覽網站生成的電子郵件,然後用戶發送它們。好的是,你可以在所有文件上使用它,而不僅僅是PHP文件。

+0

哇,這是一個非常整潔的解決方案 - 我不知道存在這樣的事情。謝謝! – Constantin

+0

這很有趣,你沒有想到它,因爲你在你的代碼中使用緩衝區。對你來說好東西,緩衝區可以嵌套,對吧? – miyasudokoro

+0

如果你可以公開訪問所有的內容/頁面,那就沒問題。否則,你可以花時間想出XML作爲控制用戶可以搜索的關鍵字的替代方法。我的想法是,更好地展示你允許的內容,而不是通過搜索所有頁面和文件夾,而不是顯示你不允許的內容。 –