Text processing-PHP reading and writing txt files

Source: Internet
Author: User
The Code is as follows: & amp; lt ;! Doctypehtml & amp; gt; & amp; lt; html & amp; gt; & amp; lt; head & amp; gt; & amp; lt; title & amp; gt; four digits. club Domain Name & amp; lt; title & amp; gt; & amp; lt; metacharset & quot; UTF-8 & quot; & amp; gt; & amp; lt; head & amp; gt; & amp; lt; body & amp; gt; & amp; lt ;? The code in the php * test.txt file is as follows:



Four. club domain names



/*
The content of the test.txt file is:

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 includes:
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 );
}
}
?>




Used to filter domain names, but how can I display only one row in the results?
Echo $ str ."
\ N "; the result is displayed normally.

Reply content:

The Code is as follows:



Four. club domain names



/*
The content of the test.txt file is:

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 includes:
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 );
}
}
?>




Used to filter domain names, but how can I display only one row in the results?
Echo $ str ."
\ N "; the result is displayed normally.

In this way:

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

Please

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

Check if it is 4

Append write:

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

I wrote a DEMO and matched it with a regular expression.

$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_contentsOverwrite the previous one each time.fwriteAppend write

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

Fopen ("test.txt", "")

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.