php把每行复制到其他文本文件里

WBOY
Release: 2016-06-23 14:17:56
Original
1093 people have browsed it

PHP 文本 行

好吧,说实话这个实在是麻烦

是这样,我要把q.txt里的文本每行按照字数复制到1.txt/2.txt/3.txt

q.txt文本格式如下:
ajsg 蓝本 草本ajsv 莫要ajsm 苹果机ajsu ?ajsc 黄果树ajsj 七星瓢虫ajsu ?ajt 蓝ajtl 蓝ajth 蔓延 惹是生非aagw 恭敬不如从命
Copy after login

要求像第一个,要把他分成两个写到2.txt里,所有汉字后面不能有空格。
如:
ajsg 蓝本ajsg 草本
Copy after login

三个字的存到3.txt里。像一下这种
ajth 蔓延 惹是生非
Copy after login

需要存到2.txt一个,4.txt一个
四字以上存到5.txt里。
1.txt,2.txt等存出来的格式要和q.txt的一样,汉字后面不能有空格
如1.txt的
a 工a 戈aa 式aaa 工aaaa 工aaar ?aad 式aad ?aadk 匿aadn 慝aadw 萁aadn 葚
Copy after login


有没有大神帮忙啊,求PHP解决方法,先谢过!


回复讨论(解决方案)

<?php$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);}?> 
Copy after login
Copy after login

不过这有一个前提;需要看你的q.txt的文档的编码是什么格式! 我这里使用UTF-8,也就是默认你的q.txt文档是UTF-8编码的;来到这里;你应该懂的了;

<?php$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);}?> 
Copy after login
Copy after login

不过这有一个前提;需要看你的q.txt的文档的编码是什么格式! 我这里使用UTF-8,也就是默认你的q.txt文档是UTF-8编码的;来到这里;你应该懂的了;
非常感谢,问题解决!我的文本就是UTF-8的。
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!