自昨晚以来,我一直在阅读/测试示例,但奶牛从未回家。
我有一个(例如)大约的文件。一行 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);
?>
但这并不像我预期的那样工作。
我意识到还有其他类似的问题,但他们没有展示如何读取文件,然后写回它。