我有一個文本文件,其中包含許多行隨機英文字母。我想檢查哪些行包含我要查找的字母。例如,我有以下文本行:pwieuthvngbcdxziaefonmlkjghbcdplmujhytrgvdsea我想知道是否有任何行包含以下字母:abcdefghijklmno,不以任何順序,所以結果將是第 2 行:iaefonmlkjghbcd到目前為止,我只有讀取文本文件行的部分:$handle = fopen("randomletters.txt", "r");if ($handle) { while (($line = fgets($handle)) !== false) { //does $line contain the letters I'm looking for? if yes print the line }}
1 回答

慕娘9325324
TA貢獻1783條經驗 獲得超4個贊
你可以使用該strpos
功能。它返回一個字符串在另一個字符串中的索引,或者FALSE
如果沒有找到的話。
$handle = fopen("randomletters.txt", "r");
$chars = str_split('abcdefghijklmno');
if ($handle) {
? ? while (($line = fgets($handle)) !== false) {
? ? ? ?$contain = true;
? ? ? ?foreach ($chars as $char) {
? ? ? ? ? ?if(strpos($line, $char) === false){
? ? ? ? ? ? ? ? $contain = false;
? ? ? ? ? ? ? ? break
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?if($contain) {
? ? ? ? ? ?return $line
? ? ? ?}
? ? }
}? ?
在這里,我還使用了該str_split函數(shù)并檢查每一行、每個字符(如果存在)。如果沒有,我們就去另一條線。如果是,我們返回該行。
- 1 回答
- 0 關注
- 142 瀏覽
添加回答
舉報
0/150
提交
取消