文本處理 - PHP讀寫txt檔案的問題

來源:互聯網
上載者:User
代碼如下:



四位.club 網域名稱



/*
test.txt 檔案內容為:

10ferents.club
1901.club
1992.club
288cash.club
2nd.club
365gold.club
3dscanning.club
406disco.club
99fitness.club
abuo9i8n3.club
abuo9i8n5.club
abur5t6b2.club
abut6y7n1.club
abut6y7n4.club
abut6y7n7.club
abuy7u8n6.club
1234.club
abcd.club

4.txt 只有:
abcd.club

正確的答案應該是:
1901.club
1992.club
1234.club
abcd.club
*/
$handle=fopen("test.txt","r");
while(!feof($handle)){
$str=fgets($handle);
if(stripos($str,".")==4){
file_put_contents("4.txt",$str);
}
}
?>




篩選網域名稱用的,但是結果怎麼只顯示一行啊,用
echo $str."
\n";顯示結果在正常的.

回複內容:

代碼如下:



四位.club 網域名稱



/*
test.txt 檔案內容為:

10ferents.club
1901.club
1992.club
288cash.club
2nd.club
365gold.club
3dscanning.club
406disco.club
99fitness.club
abuo9i8n3.club
abuo9i8n5.club
abur5t6b2.club
abut6y7n1.club
abut6y7n4.club
abut6y7n7.club
abuy7u8n6.club
1234.club
abcd.club

4.txt 只有:
abcd.club

正確的答案應該是:
1901.club
1992.club
1234.club
abcd.club
*/
$handle=fopen("test.txt","r");
while(!feof($handle)){
$str=fgets($handle);
if(stripos($str,".")==4){
file_put_contents("4.txt",$str);
}
}
?>




篩選網域名稱用的,但是結果怎麼只顯示一行啊,用
echo $str."
\n";顯示結果在正常的.

這樣:

$str = fgets($handle); $str = trim($str);

不知道的怎麼解決的時候,請

var_dump(stripos($str,"."));

看看是否是4

還有追加寫入:

file_put_contents("4.txt",$str, FILE_APPEND);

寫了個DEMO,用正則匹配的

$str = '10ferents.club 1901.club 1992.club 288cash.club 2nd.club 365gold.club 3dscanning.club 406disco.club 99fitness.club abuo9i8n3.club abuo9i8n5.club abur5t6b2.club abut6y7n1.club abut6y7n4.club abut6y7n7.club abuy7u8n6.club 1234.club abcd.club';preg_match_all('/[0-9a-z-]{4}/', $str, $arr);$string = implode("\n", $arr[0]);file_put_contents('1.txt', $string);

:

file_put_contents每次都會覆蓋掉之前的,應該用fwrite追加寫入

file_put_contents("4.txt",$str,FILE_APPEND);

fopen("test.txt","a")

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.