PHP-stripos()不处理文件内容-始终返回false
我正在为我的网站构建一个非常简单的搜索功能,但奇怪的是,PHP-stripos()不处理文件内容-始终返回false,php,Php,我正在为我的网站构建一个非常简单的搜索功能,但奇怪的是,stripos功能的表现与它完全不同。我使用了gettype,is_string,settype,以及许多其他函数来响应我所采取的每一步,在我找到变量$found之前,这一切似乎都很有效。我读过很多其他与此相关的问题,但我似乎找不到答案。我在某个地方读到,当在strpos函数中使用变量作为指针时,我需要将其加上引号,因此我对我的stripos做了这样的操作,但这并没有解决问题 还有一个附带的问题:为什么我需要将$search变量放在prin
stripos
功能的表现与它完全不同。我使用了gettype
,is_string
,settype
,以及许多其他函数来响应我所采取的每一步,在我找到变量$found
之前,这一切似乎都很有效。我读过很多其他与此相关的问题,但我似乎找不到答案。我在某个地方读到,当在strpos
函数中使用变量作为指针时,我需要将其加上引号,因此我对我的stripos
做了这样的操作,但这并没有解决问题
还有一个附带的问题:为什么我需要将$search
变量放在print\r
和htmlspecialchars
中?如果我不这样做,然后尝试使用gettype()
进行回显,它将返回NULL。。。?提前谢谢。守则:
<?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
”而不是“$\u GET['search']
直接传递到”stripos
函数。直接传递$search:$found=stripos($fileContents,$search)代码>我已经试过了,但它仍然返回false stillTryexit(var_dump($search))
并查看什么是$search。只是出于好奇,如果在stripos中用$_GET['search']替换$search,它会失败吗?另外,你确定$search在到达stripos时不为空吗?@TheonendonlyChemistryblob谢谢,你说得对