Php 文本换行并保存 txt 文件
Php Text wrap and save txt file
我有个小问题。
我想包装文本并保存到 txt 文件。
例如文本是
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.
我想每 40 个字换行并保存一个 txt 文件。
所以第一个 txt 文件:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
第二个 txt 文件:
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait
就会这样
我试过这段代码,但我不知道换行。
$text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et.";
$word = explode(" ",$text);
$count = count($word);
$limit = 40;
if($count <= $limit) { $cut = $count*50/100; }
else { $cut = $limit; }
for($i = 0; $i <= $cut; $i++)
{ $text1= $word[$i].' ' ; echo $text1;}
感谢您的帮助。
圣诞快乐,新年快乐。
使用 file_put_contents() 将文本存储到文本 (.txt) 文件中
参考这个 link : http://php.net/manual/en/function.file-put-contents.php
即:
$file = "/folder_path/name_of_file.txt";
file_put_contents($file, print_r($_GET['data'], true), FILE_APPEND);
并且您必须对要存储文本文件的文件夹授予权限。
嗯,你可以使用wordwrap
功能。此函数会在每X个字符处打断字符串,插入一个字符进行分割。
例如,在这段代码中,我想每 20 个字符断开一次字符串:
$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "\n");
return 将是:
The quick brown fox
jumped over the lazy
dog.
所以字符串上的每个部分都由 \n
分隔。现在您可以使用 explode
将字符串的各个部分放入数组中
$newtext = explode("\n", $newtext);
这将是我的带有损坏字符串的数组:
array(3) {
[0] =>
string(19) "The quick brown fox"
[1] =>
string(20) "jumped over the lazy"
[2] =>
string(4) "dog."
}
保存文件可以看这个教程:
$charnum="100";
$number=strlen($text);
$tekrar=$number/$charnum;
$newtext= str_replace("\n", "", $text);
$newtext = wordwrap($newtext, $charnum, "\n");
$newtext = explode("\n", $newtext);
for ( $count=1 ; $count < $tekrar ; $count++ )
{
$file = "/files/$count.txt";
file_put_contents($file,$newtext[$count]);
我是这样写的,但这次我接受了警告:
Warning: file_put_contents(/files/1.txt): failed to open stream: No such file or directory in
我有个小问题。 我想包装文本并保存到 txt 文件。 例如文本是
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.
我想每 40 个字换行并保存一个 txt 文件。
所以第一个 txt 文件:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
第二个 txt 文件:
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait
就会这样
我试过这段代码,但我不知道换行。
$text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et.";
$word = explode(" ",$text);
$count = count($word);
$limit = 40;
if($count <= $limit) { $cut = $count*50/100; }
else { $cut = $limit; }
for($i = 0; $i <= $cut; $i++)
{ $text1= $word[$i].' ' ; echo $text1;}
感谢您的帮助。 圣诞快乐,新年快乐。
使用 file_put_contents() 将文本存储到文本 (.txt) 文件中 参考这个 link : http://php.net/manual/en/function.file-put-contents.php
即:
$file = "/folder_path/name_of_file.txt";
file_put_contents($file, print_r($_GET['data'], true), FILE_APPEND);
并且您必须对要存储文本文件的文件夹授予权限。
嗯,你可以使用wordwrap
功能。此函数会在每X个字符处打断字符串,插入一个字符进行分割。
例如,在这段代码中,我想每 20 个字符断开一次字符串:
$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "\n");
return 将是:
The quick brown fox
jumped over the lazy
dog.
所以字符串上的每个部分都由 \n
分隔。现在您可以使用 explode
将字符串的各个部分放入数组中
$newtext = explode("\n", $newtext);
这将是我的带有损坏字符串的数组:
array(3) {
[0] =>
string(19) "The quick brown fox"
[1] =>
string(20) "jumped over the lazy"
[2] =>
string(4) "dog."
}
保存文件可以看这个教程:
$charnum="100";
$number=strlen($text);
$tekrar=$number/$charnum;
$newtext= str_replace("\n", "", $text);
$newtext = wordwrap($newtext, $charnum, "\n");
$newtext = explode("\n", $newtext);
for ( $count=1 ; $count < $tekrar ; $count++ )
{
$file = "/files/$count.txt";
file_put_contents($file,$newtext[$count]);
我是这样写的,但这次我接受了警告:
Warning: file_put_contents(/files/1.txt): failed to open stream: No such file or directory in