2016-09-06 24 views
1

我想搜索包含突出字母的文本文件中的特定數據。我用這個代碼:PHP搜索文本文件,然後回顯它

<?php 
    $file = 'textfile.txt'; 
    $searchfor = 'key'; 


    // 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 utf8_encode(implode("\n", $matches[0])); 
    } 
    else 
    { 
     echo utf8_encode("No matches found"); 
    } 
?> 

但是它是區分大小寫的,並且不適用於重音字母。

有人可以幫助我嗎?

謝謝:)

+0

使用$ pattern =「/^.*$pattern.*\$/mi」或模式不區分大小寫 –

+0

@Barmar這個問題並沒有像你說的那樣重複。在從file_get_content獲取數據時,我們必須考慮一些方面。所以請刪除**重複**。 – Manish

+0

@Barmar我想給這個問題的答案。但由於重複的內容我不能發佈在那裏。 – Manish

回答

0

@ Hiroo17予解釋的方法來做到這一點的範圍內。

假設你有textfile.txt文件,其中有像下面那樣的加重字母。

Éric Cantona kÉy

以下是處理突出字母的腳本。

$searchfor = 'key'; 
function file_get_contents_utf8($fn) { 
    $content = file_get_contents($fn); 
    return mb_convert_encoding($content, 'UTF-8', 
    mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true)); 
} 

function normalizeChars($s) { 
    $replace = array(
    'ъ'=>'-', 'Ь'=>'-', 'Ъ'=>'-', 'ь'=>'-', 
    'Ă'=>'A', 'Ą'=>'A', 'À'=>'A', 'Ã'=>'A', 'Á'=>'A', 'Æ'=>'A', 'Â'=>'A', 'Å'=>'A', 'Ä'=>'Ae', 
    'Þ'=>'B', 
    'Ć'=>'C', 'ץ'=>'C', 'Ç'=>'C', 
    'È'=>'E', 'Ę'=>'E', 'É'=>'E', 'Ë'=>'E', 'Ê'=>'E', 
    'Ğ'=>'G', 
    'İ'=>'I', 'Ï'=>'I', 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
    'ש'=>'w', 'ŵ'=>'w', 'Ŵ'=>'w', 
    'ы'=>'y', 'ŷ'=>'y', 'ý'=>'y', 'ÿ'=>'y', 'Ÿ'=>'y', 'Ŷ'=>'y', 
    'Ы'=>'y', 'ž'=>'z', 'З'=>'z', 'з'=>'z', 'ź'=>'z', 'ז'=>'z', 'ż'=>'z', 'ſ'=>'z', 'Ж'=>'zh', 'ж'=>'zh' 
    ); 
    return strtr($s, $replace); 
} 

$contents = file_get_contents_utf8($file); 

$contents = normalizeChars($contents); 

// escape special characters in the query 

$pattern = preg_quote($searchfor, '/'); 

// finalise the regular expression, matching the whole line 

$pattern = "/^.*$pattern.*\$/mi"; 

    // search, and store all matching occurences in $matches 
    if(preg_match_all($pattern, $contents, $matches)) 
    { 
     echo utf8_encode(implode("\n", $matches[0])); 
    } 
    else 
    { 
     echo utf8_encode("No matches found"); 
    } 

現在我解釋爲什麼我使用上述方法。 當您調用file_get_content時。它會破壞UTF8編碼。對於這個要麼你可以使用file_get_contents_utf8功能使用上述mb_convert_encoding或直使用utf8_encode$contents = utf8_encode(file_get_contents_utf8($file));

然後我用normalizeChars函數來處理強調字母。在我使用strtr功能,其主要工作是翻譯字符或替換子字符串。

我希望這能解決您的問題。

再次感謝Thanx @Barmar重新提出這個問題。我希望你不會對我的回答感到失望。

+0

謝謝!這解決了這個問題。 :) – Hiroo17

+0

@ Hiroo17太好了。如果解決了,請接受答案爲已接受。 – Manish

0

添加我與您目前的模式。

$pattern = "/^.*$pattern.*\$/mi"; 
+0

謝謝,現在它不區分大小寫,但它仍然不適用於突出顯示的字母。 – Hiroo17

0

您可以使用它來獲取所有包含加重字母的字符串。

preg_match_all("/\s+(.*?[ÇÜ]+.*?)\s+/i", $str, $matches); 

[CU]是C和U

之間字符有關該範圍更細節檢查ASCII table