2013-08-12 3 views
0

나는 txt 파일에서 검색 할 PHP 파일을 가지고 있으며 결과를 표시합니다. 나는 TXT에 사과 주스를PHP에서 대문자와 소문자 단어를 검색 하시겠습니까?

: 단어는 대문자 텍스트와 소문자와 같은 단어에 대한 사용자 검색으로 작성된 경우

그러나이 표시됩니다 PHP 파일은 예를 들어 어떤 matches1

발견 파일. 사용자가 사과 주스를 검색합니다. PHP에서 대문자가있는 "Apple Juice"라는 단어를 정확히 찾고 있기 때문에 일치하는 항목이 없습니다.

<html> 
<head><title>some title</title></head> 
<body> 

<?php 
    if(!empty($_POST['search'])) { 
    $file = 'mytxtfile.txt'; 
    $searchfor = ''; 
    // the following line prevents the browser from parsing this as HTML. 
    header('Content-Type: text/plain'); 
    $searchfor = $_POST['search']; 
    $contents = file_get_contents($file); 
    $pattern = preg_quote($searchfor, '/'); 
    $fullword = '\b\Q' . $w . '\E\b'; 
    $regex = '/' . $fullword . '(?!.*' . $fullword . ')/i'; 
    $pattern = "/^.*$pattern.*\$/m"; 
    if(preg_match_all("/\b([a-z]+[A-Z]+[a-zA-Z]*|[A-Z]+[a-z]+[a-zA-Z]*)\b/", $pattern, $contents, $matches)){ 
     echo "Population: \n"; 
     echo implode("\n", $matches[0]); 

    } 
    else{ 
     echo "No matches found"; 
    } 
    header('Content-Type: text/html'); 
    } 
?> 

    <form method="post" action=""> 
    <input type="text" name="search" /> 
    <input type="submit" name="submit" /> 
    </form> 

</body> 
</html> 

내가 시도하고 내 코드이 if(preg_match_all("/\b([a-z]+[A-Z]+[a-zA-Z]*|[A-Z]+[a-z]+[a-zA-Z]*)\b/",을 추가하지만이 작동하지 않았다 않았다

이 내 코드입니다!

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

감사합니다.

+3

하지 당신은 단지에 관계없이 전체를 소문자 이유는 무엇입니까. 이렇게 대소 문자를 구별합니다. 그렇지 않으면 regexp에 대문자와 소문자를 구별하기 위해 추가 할 수있는 'i' 플래그가 있습니다. – CP510

+1

'$ contents = strtolower (file_get_contents ($ file));' – CP510

+0

이 질문은 귀하의 질문과 관련이 없지만 나는 궁금합니다. 이'headers already sent'와 같은 경고 메시지를받지 않고 코드가 작동합니까? –

답변

1

조정 된 소스를 강조합니다. 이렇게하면 검색 문자열과 파일 내용이 소문자로 설정됩니다. HTML 헤더를 아래로 이동하고 검색 논리 중에 출력 버퍼를 시작했습니다.

<?php 
ob_start(); 
if(!empty($_POST['search'])) { 
$file = 'mytxtfile.txt'; 
$searchfor = ''; 
// the following line prevents the browser from parsing this as HTML. 
header('Content-Type: text/plain'); 
$searchfor = strtolower($_POST['search']); #LOWER CASE THE SEARCH STRING TO 
$contents = strtolower(file_get_contents($file)); #MAIN ADDITION TO MAKE IT LOWER CASE 
$pattern = preg_quote($searchfor, '/'); 
$fullword = '\b\Q' . $w . '\E\b'; 
$regex = '/' . $fullword . '(?!.*' . $fullword . ')/i'; 
$pattern = "/^.*$pattern.*\$/m"; 
if(preg_match_all("/\b([a-z]+[A-Z]+[a-zA-Z]*|[A-Z]+[a-z]+[a-zA-Z]*)\b/", $pattern,    $contents, $matches)){ 
    echo "Population: \n"; 
    echo implode("\n", $matches[0]); 

} 
else{ 
    echo "No matches found"; 
} 
header('Content-Type: text/html'); 
} 
ob_end_flush(); 
?> 
<html> 
<head><title>some title</title></head> 
<body> 

<form method="post" action=""> 
    <input type="text" name="search" /> 
    <input type="submit" name="submit" /> 
</form> 

</body> 
</html> 
+0

그러나 아름답게 작동하고, 잘못 된 코드가 아니므로 계속해서 일치하는 항목이 없습니다. 그러나 좋은 일, 내 부분에 +1. 건배 –

+0

나는 사용자가 대문자를 사용하지 않는 한이 기능이 작동한다고 말했기 때문에! –

+0

입력 및 파일 내용을 모두 소문자로 설정하도록 코드를 편집했습니다. 따라서 대소 문자를 구분하지 않아야합니다. – CP510

1
이처럼 정규식 패턴을 변경

:

// The "i" at the end is to make a case-insensitive search 
$pattern = "/^.*$pattern.*\$/mi"; 
관련 문제