PHP reads data from a TXT data, matches the conditions after writing to another TXT file

Source: Internet
Author: User
PHP reads data from a TXT file, matches the conditions after writing to another TXT file
I want to use PHP from a TXT file to read the data, matching the conditions of the write to another TXT file, the code, do not know where the problem, after each execution will only write the last one to meet the criteria (on one), I hope that all meet the conditions, I hope the hero guidance!


$path = ' aa.txt ';

$FP =fopen ($path, "R");


$str = file_get_contents ($path);//Get content
$arr = explode ("\ n", $str);//Branch deposit Array

foreach ($arr as $row)//traverse each row
{
if (Preg_match ("/^ ([a-za-z0-9_\.\-]) +\@ (([a-za-z0-9\-]) +\.) + ([a-za-z0-9]{2,4}) +$/", $row)) {
$path 2 = ' bb.txt ';
$FP 2=fopen ($path 2, "a");
Fwrite ($fp 2, $row. " \ r \ n ");
Fclose ($fp 2);
}
}
Fclose ($FP);
?>

------Solution--------------------
There's no problem with your code.

Why don't you just take out the email, please?

$str = file_get_contents ($path);//Get 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 (' $path 2, join ("\ n", $r [0]));
}

You can do it.
------Solution--------------------

$arr = explode ("\ n", $str);
Change into
$arr = Preg_split ('/[\\r\\n]+/', $str);
  • 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.