필자는 PHP를 통해 txt 파일을 읽고 사용자가 텍스트 필드를 통해 검색 한 내용에 따라 결과를 표시 할 수 있어야합니다.사용자가 검색 한 키워드에 따라 PHP를 통해 txt 파일을 읽으시겠습니까?
이 PHP 코드를 발견했지만 어떻게 텍스트 필드 상자를 구현할 수 있는지 알지 못합니다.
<?php
$file = 'somefile.txt';
$searchfor = '';
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
echo implode("\n", $matches[0]);
}
else{
echo "No matches found";
}
?>
나는 이런 식으로 뭔가를 시도했지만 작동하지 않았다 :
<form method="post" action="">
<input type="text" name="something" value="<?php $searchfor ?>" />
<input type="submit" name="submit" />
</form>
어떤 도움을 주시면 감사하겠습니다.
이 또한 작동하지만 PHP는 페이지로드시 모든 txt 파일 내용을 표시합니다! 내가 그 일을 어떻게 막을 수 있는지 알고 있니? –
예 대답을 참조하십시오 – meda