PHP reads data from one TXT file, and writes the matching data to another TXT file.

Source: Internet
Author: User
PHP reads data from a TXT file. after matching, it writes data to another TXT file. I want to use PHP to read data from a TXT file. after matching, it writes data to another TXT file, I don't know what the problem is. after each execution, I only write the last one that meets the conditions (just one). I hope all the conditions are met. I hope you can give me some advice! & Lt ;? Php $ path = 'AA. tx PHP reads data from a TXT file and writes the matching data to another TXT file.
I want to use PHP to read data from a TXT file, and write the matching data to another TXT file. The following is the code and I don't know what the problem is, after each execution, only the last qualified (just one) will be written. I want all qualified, and hope you can give me some advice!


$ Path parameter 'aa.txt ';

$ Fp = fopen ($ path, "r ");


$ Str = file_get_contents ($ path); // Obtain the content
$ Arr = explode ("\ n", $ str); // store a branch to an array

Foreach ($ arr as $ row) // traverses each row
{
If (preg_match ("/^ ([a-zA-Z0-9 _\. \-]) + \ @ ([a-zA-Z0-9 \-]) + \.) + ([a-zA-Z0-9] {2, 4}) + $/", $ row )){
$ Path2 then 'bb.txt ';
$ Fp2 = fopen ($ path2, "");
Fwrite ($ fp2, $ row. "\ r \ n ");
Fclose ($ fp2 );
}
}
Fclose ($ fp );
?>

------ Solution --------------------
You can't see the problem from your code.

Didn't you just retrieve the email?

$ Str = file_get_contents ($ path); // Obtain the content
If (preg_match_all ("/^ ([a-zA-Z0-9 _\. \-]) + \ @ ([a-zA-Z0-9 \-]) + \.) + ([a-zA-Z0-9] {2, 4}) + $/m ", $ str, $ r )){
File_put_contents ('$ path2, join ("\ n", $ r [0]);
}

You can.
------ Solution --------------------

$ Arr = explode ("\ n", $ str );
Change
$ Arr = preg_split ('/[\ r \ n] +/', $ str );

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.