我正在为我的网站构建一个非常简单的搜索功能,但奇怪的是stripos
功能并不像它那样。我已经使用gettype
,is_string
,settype
以及许多其他功能来回应我所采取的每一步,这一切似乎都有效,直到我找到变量{{1 }}。我已经阅读了许多与此相关的其他问题,但我似乎无法找到答案。我在某处读到,当$found
函数中使用变量作为针时,我需要将它放在引号中,所以我为我的 strpos
做了这个,但是那并没有#39;解决问题。
另外,只是一个附带问题:为什么我需要将stripos
变量放在$search
和print_r
中?如果我没有,然后尝试用htmlspecialchars
回显,它会返回NULL ...?提前致谢。代码:
gettype()
已解决:必须将<?php
/*------------------------VARIABLES------------------------*/
$search = $_GET['search'];
$numberOfResults = 0;
$fileContents;
$found;
$contentsLength;
$startSearch;
$endSearch;
$contentsSearchLength;
/*------------------------FUNCTIONS------------------------*/
function displaySearch() {
//I'd have thought I don't need the below line but it returns NULL if I don't... found the script somewhere in the docs
$search = htmlspecialchars(print_r($search, true));
foreach (glob('search/this/directory/*.*') as $file) {
$fileContents = file_get_contents($file);
//these next five lines narrow the file contents down to the body of the content of the file -- I don't want to search scripts, styling, or links!
$contentsLength = strlen($fileContents);
$startSearch = strpos($fileContents, '<div id="body">');
$endSearch = strpos($fileContents, '<!--FOOTER-->');
$contentsSearchLength = $contentsLength - ($contentsLength - $endSearch) - $startSearch;
$fileContents = strip_tags(substr($fileContents, $startSearch, $contentsSearchLength));
$numberOfResultsInFile = 0;
$found = stripos($fileContents, "$search");
if ($found !== false) {
$numberOfResults += 1;
echo '<div class="results" id="result'.$numberOfResults.'">';
echo '<div class="title">';
echo '<a target="_blank" href="'.$file.'">Result'.$numberOfResults.'</a>';
echo '</div>';
echo '<div id="preview">';
echo 'Preview of file contents here';
echo '</div>';
echo '</div>';
} else {
//move on to next file
}
}
if ($numberOfResults == 0) {
echo 'No results could be found';
}
}
?>
<h1>Search Results for "<?php echo $search ?>":</h1>
<br>
<div id="results">
<?php
if (empty($search)) {
echo 'Please enter a search query or go back to the <a href="home.php">Home Page</a>';
} else {
displaySearch();
}
?>
</div>
,$search
直接传递给$_GET['search']
函数。