我是一名 Java 開發人員,正在努力撰寫他的第一個 PHP 腳本。僅供參考,我在 Ubuntu 機器上使用 PHP 8.1.2 進行編碼。
我的代碼必須打開一個日志檔案,逐一讀取行,然后根據字串的前導碼提取關鍵子字串。例如,如果日志檔案是:
April 01 2020 Key Information Read :: Interesting Character #1: Kermit the Frog
April 01 2020 Key Information Read :: Interesting Character #2: Miss Piggy
April 01 2020 Key Information Read :: Their Best Movie: The Muppet Movie (1979)
...many more lines...
然后我需要一個腳本來讀取每一行并提取:
Kermit the Frog
Miss Piggy
The Muppet Movie (1979)
...many more items...
在讀取檔案之前,我不會知道上述值。
這個問題非常好解決。這是我的 PHP 代碼,其中 $str 是輸入檔案的一行:
function parseThisStr($str){
if( str_contains($str, "Interesting Character #1: ") ){
$mySubstr = "Interesting Character #1: ";
$tmpIndex = strpos( $str, $mySubstr );
$tmpIndex = strlen($mySubstr);
$str2 = substr( $str, $tmpIndex );
$str2 = preg_replace('~[\r\n] ~', '', $str2); // remove newline
return $str2;
}
else if( str_contains($str, "Interesting Character #2: ") ){
$mySubstr = "Interesting Character #2: ";
...copy code from above...
return $str2;
else if( str_contains($str, "Their Best Movie: ") ){
$mySubstr = "Their Best Movie: ";
...copy code from above...
return $str2;
return $str;
}
這會起作用......但它不必要的重復,對吧?對于我要檢查的每個子字串,我需要復制五行相同的代碼。我需要搜索大約 30 個子字串;這將使我的代碼比它需要的長約 150 行。
必須有一種更智能的方法來做到這一點,對吧?我不能將每個要搜索的子字串存盤在一個陣列中,可能是這樣的:
$array = array(
1 => "Interesting Character #1: ",
2 => "Interesting Character #2: ",
3 => "Their Best Movie: ",
...etc...
);
...然后遍歷陣列,可能是這樣的:
function parseThisStr($str){
$array = array(
1 => "Kermit the Frog",
...etc...
};
foreach( $array as &$value ){
if( str_contains($str, $value) ){
$tmpIndex = strpos( $str, $value );
$tmpIndex = strlen($value);
$str2 = substr( $str, $tmpIndex );
$str2 = preg_replace('~[\r\n] ~', '', $str2); // remove newline
return $str2;
}
return null;
}
從概念上講,這應該可行......但我無法弄清楚正確的語法。可悲的是,PHP 語法讓我感到困惑。有誰知道我哪里出錯了?謝謝你。
$array編輯:我在第一次發帖 時搞砸了 的價值觀。$array應該有我將用來搜索較大字串的子字串。
uj5u.com熱心網友回復:
您可以使用具有更具體模式的正則運算式:
\b(?:Interesting Character #\d :|Their Best Movie:)\h \K.
模式匹配:
\b防止部分單詞匹配的單詞邊界(?:Interesting Character #\d :|Their Best Movie:)\h匹配 1 個水平空白字符\K忘記到目前為止匹配的內容.匹配 1 個或多個字符
查看正則運算式演示和PHP 演示
$re = '/\b(?:Interesting Character #\d :|Their Best Movie:)\h \K. /';
$str = 'April 01 2020 Key Information Read :: Interesting Character #1: Kermit the Frog
April 01 2020 Key Information Read :: Interesting Character #2: Miss Piggy
April 01 2020 Key Information Read :: Their Best Movie: The Muppet Movie (1979)
';
preg_match_all($re, $str, $matches);
print_r($matches[0]);
輸出
Array
(
[0] => Kermit the Frog
[1] => Miss Piggy
[2] => The Muppet Movie (1979)
)
另一種具有更廣泛匹配的模式,考慮到前導::并匹配直到第一次出現:
::\h [^:\r\n] :\h \K.
查看另一個正則運算式演示
uj5u.com熱心網友回復:
使用正則運算式將產生更清晰的代碼。例如preg_match:
$line = 'April 01 2020 Key Information Read :: Interesting Character #1: Kermit the Frog';
$searchTerms = ["Kermit the Frog","Miss Piggy","The Muppet Movie (1979)"];
// prepare regex with named group from terms
$delimiter = '~';
$regex = $delimiter . '(?<phrase>(' . join('|', array_map(fn($term) => preg_quote($term, $delimiter), $searchTerms)) . '))' . $delimite;
// search by regex
preg_match($regex, $line, $matches);
$foundPhrase = $matches['phrase'] ?? null;
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/461366.html
