Text Processing-PHP read and write TXT file problems

Source: Internet
Author: User
The code is as follows:



<title>Four-bit. Club Domain</title>



/*
The contents of the Test.txt file are:

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 only:
Abcd.club

The correct answer should be:
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);
}
}
?>




Filter the domain name, but the results show only one line, with
echo $str. "
\ n "; display results in normal.

Reply content:

The code is as follows:



Four-bit. Club Domain



/*
The contents of the Test.txt file are:

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 only:
Abcd.club

The correct answer should be:
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);
}
}
?>




Filter the domain name, but the results show only one line, with
echo $str. "
\ n "; display results in normal.

Such

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

Do not know how to solve the time, please

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

See if it's 4.

There are also append writes:

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

Wrote a demo, with regular matches.

$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_contentsEach time it is overwritten, it should be written with an fwrite append

File_put_contents ("4.txt", $str, File_append);

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

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.