Php copies each line to other text files

Source: Internet
Author: User
The text format of examples/2.txt/ 3.txtq.txt is as follows: ajsg & nbsp; blueprint & nbsp; herbal ajsv & nbsp; Mo Yao ajsm & nbsp; macOS ajsu & nbsp; php copies each line to other text files
Okay. To be honest, this is really troublesome.

In this case, copy the number of tokens in q.txt to 1.txt/2.txt/ 3.txt.

The q.txt text format is as follows:

Ajsg blueprint herb
Ajsv Moyao
Ajsm Apple
Ajsu logs
Ajsc Huangguoshu
Ajsj Dipper Ladybug
Ajsu logs
Ajt Blue
Ajtl Blue
Ajth spread
Aagw is respectful

For the first character, separate the two characters and write them into 2.txt. no spaces are left behind all Chinese characters.
For example:

Ajsg blueprint
Ajsg herb

The three characters are stored in 3.txt. Like This

Ajth spread

The token is saved to 2.txt and 4.txt.
Four characters are stored in 5.txt.
The format of 1.txt,2.txtmust be the same as that of q.txt.
For example, 1.txt

Work
A GE
Aa type
Aaa worker
Aaaa worker
Aaar labels
Aad type
Aad connector
Aadk plugin
Aadn subnet
Aadw release
Aadn subnet


Is there any great Help? please ask for a PHP solution. thank you! PHP text lines are shared:
------ Solution --------------------

$handle = @fopen("q.txt", "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        $data=explode(' ',trim($buffer));
        $tap=trim(array_shift($data));
foreach($data as $v){
$encode = 'UTF-8';
$str_num = mb_strlen(trim($v),$encode);
$fname=$str_num>4?5:$str_num;
@file_put_contents($fname.".txt",$tap." ".trim($v)."\r\n",FILE_APPEND);
}
    }
    fclose($handle);
}
?> 

Let's look at the encoding format of your q.txt document! I am using or above your q.txt document is UTF-8 code; come here; you should understand;

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.