Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将内容分成相等的部分,然后在PHP中写入文件_Php_File_Split - Fatal编程技术网

如何将内容分成相等的部分,然后在PHP中写入文件

如何将内容分成相等的部分,然后在PHP中写入文件,php,file,split,Php,File,Split,从昨晚开始,我就一直在阅读/测试示例,但奶牛从未回家 我有一个文件(例如)一行大约有1000个字符,我想把它分成10个相等的部分,然后写回该文件 目标: 1. Open the file in question and read its content 2. Count up to 100 characters for example, then put a line break 3. Count 100 again and another line break, and so on till

从昨晚开始,我就一直在阅读/测试示例,但奶牛从未回家

我有一个文件(例如)一行大约有1000个字符,我想把它分成10个相等的部分,然后写回该文件

目标:

1. Open the file in question and read its content
2. Count up to 100 characters for example, then put a line break
3. Count 100 again and another line break, and so on till it's done.
4. Write/overwrite the file with the new split content
I want to turn this => KNMT2zSOMs4j4vXsBlb7uCjrGxgXpr

Into this:

KNMT2zSOMs
4j4vXsBlb7
uCjrGxgXpr
<?php
$MyString = fopen('file.txt', "r");

$MyNewString;
$n = 100; // How many you want before seperation
$MyNewString = substr($MyString,0,$n); 
$i = $n;
while ($i < strlen($MyString)) {
$MyNewString .= "\n"; // Seperator Character
$MyNewString .= substr($MyString,$i,$n);
$i = $i + $n;
}
file_put_contents($MyString, $MyNewString);

fclose($MyString);
?>
例如:

1. Open the file in question and read its content
2. Count up to 100 characters for example, then put a line break
3. Count 100 again and another line break, and so on till it's done.
4. Write/overwrite the file with the new split content
I want to turn this => KNMT2zSOMs4j4vXsBlb7uCjrGxgXpr

Into this:

KNMT2zSOMs
4j4vXsBlb7
uCjrGxgXpr
<?php
$MyString = fopen('file.txt', "r");

$MyNewString;
$n = 100; // How many you want before seperation
$MyNewString = substr($MyString,0,$n); 
$i = $n;
while ($i < strlen($MyString)) {
$MyNewString .= "\n"; // Seperator Character
$MyNewString .= substr($MyString,$i,$n);
$i = $i + $n;
}
file_put_contents($MyString, $MyNewString);

fclose($MyString);
?>
这就是我目前的情况:

1. Open the file in question and read its content
2. Count up to 100 characters for example, then put a line break
3. Count 100 again and another line break, and so on till it's done.
4. Write/overwrite the file with the new split content
I want to turn this => KNMT2zSOMs4j4vXsBlb7uCjrGxgXpr

Into this:

KNMT2zSOMs
4j4vXsBlb7
uCjrGxgXpr
<?php
$MyString = fopen('file.txt', "r");

$MyNewString;
$n = 100; // How many you want before seperation
$MyNewString = substr($MyString,0,$n); 
$i = $n;
while ($i < strlen($MyString)) {
$MyNewString .= "\n"; // Seperator Character
$MyNewString .= substr($MyString,$i,$n);
$i = $i + $n;
}
file_put_contents($MyString, $MyNewString);

fclose($MyString);
?>

但这并不像我预期的那样有效


我意识到还有其他类似的问题,比如我的问题,但它们没有说明如何读取文件,然后再写回文件。

请查看文档以了解更多信息。它将获取一个字符串,并根据长度将其拆分为块,将每个块存储在它返回的数组中的单独索引中。然后,您可以在数组上迭代,在每个索引后添加一个换行符。

请查看文档以了解。它将获取一个字符串,并根据长度将其拆分为块,将每个块存储在它返回的数组中的单独索引中。然后可以在数组上迭代,在每个索引后添加换行符。


<?php

$str = "aonoeincoieacaonoeincoieacaonoeincoieacaonoeincoieacaonoeincoieacaon";

$pieces = 10;

$ch = chunk_split($str, $pieces);
$piece = explode("\n", $ch);
foreach($piece as $line) {
// write to file
}

?>



在这里等一下。您没有为
file\u put\u contents()提供文件名/路径,您正在提供一个文件句柄

试试这个:

file_put_contents("newFileWithText.txt", $MyNewString);
你看,在执行
时,$var=fopen()
,您正在给
$var
一个句柄值,该值不用于
文件\u put\u contents()因为它不要求句柄,而是要求文件名。所以,它应该是:
file\u put\u内容(“myfilenamehere.txt”,“我想要在我的文件中的数据…”)


简单。

在这里等一下。您没有为
file\u put\u contents()提供文件名/路径,您正在提供一个文件句柄

试试这个:

file_put_contents("newFileWithText.txt", $MyNewString);
你看,在执行
时,$var=fopen()
,您正在给
$var
一个句柄值,该值不用于
文件\u put\u contents()因为它不要求句柄,而是要求文件名。所以,它应该是:
file\u put\u内容(“myfilenamehere.txt”,“我想要在我的文件中的数据…”)


很简单。

那么问题出在哪里呢?读/写文件?还是拆分字符串?@navnav将拆分内容写回文件。我刚才试过这个
文件内容($MyString,$MyNewString)但它不起作用。@navnav它起作用了
echo$MyNewString但这不会
文件内容($MyString,$MyNewString)我做错了什么?@nav检查/回复,再次感谢。干杯~那么问题出在哪里呢?读/写文件?还是拆分字符串?@navnav将拆分内容写回文件。我刚才试过这个
文件内容($MyString,$MyNewString)但它不起作用。@navnav它起作用了
echo$MyNewString但这不会
文件内容($MyString,$MyNewString)我做错了什么?@nav检查/回复,再次感谢。干杯!成功!谢谢你,还有添加的信息。成功!谢谢你的帮助,还有添加的信息。