将 PHP 中的输入字符串拆分为多个部分而不打断单词
Split input string in PHP into multiple parts without breaking words
已设法获取一个输入字符串并将其分成两部分并将其写入 2 个文件。我现在想要实现的是当它大于我的限制时能够将它分成 3 甚至 4 个部分并将该数据写入单独的文件而不破坏输入。
这是我在这个问题上找到的到目前为止所管理的内容:Split Strings in Half (Word-Aware) with PHP
public function createfiles(array $lines)
{
$File1 = __DIR__ . '/file1.txt';
$File2 = __DIR__ . '/file2.txt';
$regexLines = [];
foreach ($lines as $line) {
$regexLines[] = preg_quote($line);
}
$data = implode('|', $regexLines);
//current data input is 18000
$myLimit = 10000;
$dataLength = strlen($data);
if ($dataLength > $myLimit) {
$middle = strrpos(substr($data, 0, floor($dataLength / 2)), '/') + 1;
//now want to split into four parts if input data is for instance 35000 characters
// Strip off trailing /
$data1 = substr($data, 0, $middle-1);
$data2 = substr($data, $middle);
//now want a $data3 and $data4 also stripping off a trailing /
$this->writeToFile($File1, $data1);
$this->writeToFile($File2, $data2);
//now want to write to $File3 and $File4 if needed
} else {
$this->writeToFile($File1, $data);
};
}
经过数小时的挖掘和摆弄,终于找到了解决方案。我向它扔了一个大清单,它为我把它很好地分成了 7 个文件,而没有把单词分成两半。
public function createmultiplefiles(array $lines)
{
$regexLines = [];
foreach ($lines as $line) {
$regexLines[] = preg_quote($line);
}
$data = implode('|', $regexLines);
$mylimit = 10000;
$datalength = strlen($data);
$lastpos = 0;
for ($x = 1; $lastpos < $datalength; $x++) {
if( ($datalength-$lastpos) >= $mylimit){
$pipepos = strrpos(substr($data, $lastpos, $mylimit), '|');
$splitdata = substr($data, $lastpos, $pipepos);
$lastpos = $lastpos + $pipepos+1;
}else{
$splitdata = substr($data, $lastpos);
$lastpos = $datalength;
}
$file = __DIR__ . 'myfile-' . $x . '.txt';
$this->writeToFile($file, $splitdata);
}
}
已设法获取一个输入字符串并将其分成两部分并将其写入 2 个文件。我现在想要实现的是当它大于我的限制时能够将它分成 3 甚至 4 个部分并将该数据写入单独的文件而不破坏输入。
这是我在这个问题上找到的到目前为止所管理的内容:Split Strings in Half (Word-Aware) with PHP
public function createfiles(array $lines)
{
$File1 = __DIR__ . '/file1.txt';
$File2 = __DIR__ . '/file2.txt';
$regexLines = [];
foreach ($lines as $line) {
$regexLines[] = preg_quote($line);
}
$data = implode('|', $regexLines);
//current data input is 18000
$myLimit = 10000;
$dataLength = strlen($data);
if ($dataLength > $myLimit) {
$middle = strrpos(substr($data, 0, floor($dataLength / 2)), '/') + 1;
//now want to split into four parts if input data is for instance 35000 characters
// Strip off trailing /
$data1 = substr($data, 0, $middle-1);
$data2 = substr($data, $middle);
//now want a $data3 and $data4 also stripping off a trailing /
$this->writeToFile($File1, $data1);
$this->writeToFile($File2, $data2);
//now want to write to $File3 and $File4 if needed
} else {
$this->writeToFile($File1, $data);
};
}
经过数小时的挖掘和摆弄,终于找到了解决方案。我向它扔了一个大清单,它为我把它很好地分成了 7 个文件,而没有把单词分成两半。
public function createmultiplefiles(array $lines)
{
$regexLines = [];
foreach ($lines as $line) {
$regexLines[] = preg_quote($line);
}
$data = implode('|', $regexLines);
$mylimit = 10000;
$datalength = strlen($data);
$lastpos = 0;
for ($x = 1; $lastpos < $datalength; $x++) {
if( ($datalength-$lastpos) >= $mylimit){
$pipepos = strrpos(substr($data, $lastpos, $mylimit), '|');
$splitdata = substr($data, $lastpos, $pipepos);
$lastpos = $lastpos + $pipepos+1;
}else{
$splitdata = substr($data, $lastpos);
$lastpos = $datalength;
}
$file = __DIR__ . 'myfile-' . $x . '.txt';
$this->writeToFile($file, $splitdata);
}
}