2015-09-22 33 views
0

所以,我一直在努力使一個搜索引擎不使用數據庫。它應該做的是在網頁中查找搜索到的單詞並自動給出鏈接。下面是它的代碼:
如何使這個搜索引擎與subsrt_count一起工作?

<?php 

session_start(); 

$searchInput = $_POST['search']; 

    $inputPage1 = $_SESSION['pOneText']; 
    $inputPage2 = isset($_SESSION['pTwoText']) ? $_SESSION['pTwoText'] : ""; 
    $inputPage3 = isset($_SESSION['pThreeText']) ? $_SESSION['pThreeText'] : ""; 

    $fUrl = file_get_contents("mDummyP.php"); 
    $sUrl = file_get_contents("sDummyP.php"); 
    $tUrl = file_get_contents("tDummyP.php"); 

    if (substr_count($fUrl, $searchInput) !== false) { 
     echo "All results for <strong> $searchInput </strong> : <br>" ; 
    } elseif (substr_count($sUrl, $searchInput) !== false) { 
     echo "All results for <strong> $searchInput </strong> : <br>"; 
    } elseif (substr_count($tUrl, $searchInput) !== false) { 
     echo "All results for <strong> $searchInput </strong> : <br>"; 
    } else { 
     echo "No resulst for <strong> $searchInput </strong>! "; 
    } 

    ?> 

但是,它從來沒有檢查,如果這個詞確實存在與否,它總是返回「所有結果」。所以,我想知道是否有人知道爲什麼或有改進它的建議。請記住,它永遠不會被專業使用,它只是爲了測試我的能力。提前致謝!

+0

IMO,這將是一個資源食者?糾正我,如果我錯了,但你想在每個查詢抓取成千上萬的網站? –

+0

不是,它是爲我的網站,引擎會搜索字符串只是在我的網站頁面,然後報告它的確切鏈接。 –

+0

你的網站內容是否是靜態的? –

回答

0

你需要看看PHP手冊substr_count

返回值

這個函數返回一個整數。

因此它總是會進入這個第一,如果塊:

if (substr_count($fUrl, $searchInput) !== false) { 

因爲substr_count的返回值只有永遠的整數且永遠不會是假的 - 你的代碼檢查的準確價值和類型false

此外,除了別的塊,所有的報表都只是echo'ing完全相同的字符串,所以你不會看到任何輸出分化,如果執行沒有進入IF或ELSEIF塊。請參閱下面的內容讓您走上正確的軌道:

$searchInput = 'some'; 

$fUrl = 'this is test file text'; 
$sUrl = 'this is some other text'; 
$tUrl = 'extra text'; 

if (substr_count($fUrl, $searchInput) !== 0) { 
    echo "a"; 
} elseif (substr_count($sUrl, $searchInput) !== 0) { 
    echo "b"; 
} elseif (substr_count($tUrl, $searchInput) !== 0) { 
    echo "c"; 
} else { 
    echo "No resulst for <strong> $searchInput </strong>! "; 
} 
+0

因此,你在建議什麼? –

+0

編輯我的答案,請再次看看,如果這有助於您請getvote並考慮標記蜱作爲您的問題的接受答案。謝謝。 – ajmedway

+0

注意:您可能沒有意識到這一點,但是**!= **和**!== **之間的php存在差異,我建議您仔細閱讀以下內容:[PHP比較運算符](http:// au.php.net/manual/en/language.operators.comparison.php) – ajmedway

相關問題