2013-08-11 3 views
-1

필자는 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> 

어떤 도움을 주시면 감사하겠습니다.

답변

0

약간 조정할 필요가 있습니다. 입력 할 값을 추가 할 필요가 없습니다. 당신은 값이 너무 value="<?php $searchfor ?>" 다음

스크립트가 있어야한다 어떻게 제거 제출됩니다 페이지를 게시 할 때 :

<?php 
    if(!empty($_POST['something'])) { 
    $file = 'form.txt'; 
    $searchfor = ''; 
    // the following line prevents the browser from parsing this as HTML. 
    header('Content-Type: text/plain'); 
    $searchfor = $_POST['something']; 
    $contents = file_get_contents($file); 
    $pattern = preg_quote($searchfor, '/'); 
    $pattern = "/^.*$pattern.*\$/m"; 
    if(preg_match_all($pattern, $contents, $matches)){ 
     echo "Found matches:\n"; 
     echo implode("\n", $matches[0]); 
    } 
    else{ 
     echo "No matches found"; 
    } 
    header('Content-Type: text/html'); 
    } 
?> 

    <form method="post" action=""> 
    <input type="text" name="something" /> 
    <input type="submit" name="submit" /> 
    </form> 
+0

이 또한 작동하지만 PHP는 페이지로드시 모든 txt 파일 내용을 표시합니다! 내가 그 일을 어떻게 막을 수 있는지 알고 있니? –

+0

예 대답을 참조하십시오 – meda

0
<?php 
$file = 'somefile.txt'; 
$searchfor = ''; 

if (!empty($_POST['something'])) { 
    // 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 = $_POST['something']; 
    // 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"; 
     var_dump($matches); 
    } 
    else{ 
     echo "No matches found"; 
    } 
    exit; // since it is parsed as plaintext, you can't use the html form below anyway 
} 

?> 

    <form method="post" action=""> 
    <input type="text" name="something" value="<?php $searchfor ?>" /> 
    <input type="submit" name="submit" /> 
    </form> 

것들 변경 : $ 경우

  1. 확인 _POST [ 'something']이 (가) 검색 전에 설정됩니다.
  2. 내가 항상 이중 슬래시를 넣었으므로 $pattern = preg_quote($searchfor, '/');이 삭제되었습니다. 다시 확인해보십시오.
  3. 실제로 $pattern에서 $pattern = preg_quote($searchfor, '/');으로 설정하십시오. 그렇지 않으면 사용자 입력을받지 못할 것입니다.
  4. 스크립트를 종료하십시오 (주석 참조).
  5. var_dump($matches)을 사용했음을 유의하십시오. 그렇지 않으면, 하위 일치를 사용할 때 올바른 결과를 얻지 못할 수도 있습니다.
+0

이것은 잘 작동하지만 반환 된 결과가 이상한 형식입니다! "[0] => array (1) {[0] => string (25)"Apple "} [1] => 배열 (11) "Apple"}} 대신 Apple 만 표시합니다! 어떤 생각? –

관련 문제