Forum Moderators: coopster
Then just use explode again on each element of the array resulting from your first explode, this time looking for the start text.
open firstfile for read
while not end of firstfile do the following:
1. while not starttext and not end of file keep reading
2. if starttext found do the following
a. open newfile // first file fo
b. while not endtext and not end of firstfile, read firstfile and write to newfile
c. close newfile
close firstfile
if endtext not found delete the last newfile created.
$content = file_get_contents($path);
$parts = explode($stoptext,$content);
$cnt = 1;
foreach($parts as $part) {
if(strpos($part,$starttext) !== 0) {
list($junk,$part) = explode($starttext,$part,2);
$part = $starttext . $part;
} // EndIf excise junk
$part .= $stoptext;
file_put_contents($path.$cnt++,$part);
} // EndForEach part
$content = file_get_contents("test.txt");
$stoptext = "test3";
$starttext = "blah";
$parts = explode($stoptext,$content);
$cnt = 1;
foreach($parts as $part) {
if(strpos($part,$starttext) !== 0) {
list($junk,$part) = explode($starttext,$part,2);
$part = $starttext . $part;
} // EndIf excise junk
$part .= $stoptext;
file_put_contents("test.txt".$cnt++,$part);
} // EndForEach part
The second element is
blahS
blah2
blahS
blah2
The third element is
blahf
blah2
blahf
blah2
And the fourth element is empty because stoptext was at the end of the content.
Your second and third sections don't contain the start text - if that's a real condition and you don't want to include them, add:
if(strpos($part,$starttext) === false)
continue;
right after the foreach.
Let's say that the second section does have the start text, but not at the beginning:
blahS
blah
blah2
blahS
blah2
The second explode will produce an array with two elements:
blahS
and
blah2
blahS
blah2
list() puts the first element into $junk which we never use. The second element gets put back into $part.
If you're looking closely, you'll notice that explode "swallows" the delimiter, so we have to put it back into the content, hence the concatenation.
To eliminate the empty file at the end (or in any spot where the stop text is repeated with no content and no start text), enclose these two lines with an if:
if($part != '') {
$part .= $stoptext;
file_put_contents($path.$cnt++,$part);
} // EndIf section not empty
} // EndForEach part
$content = file($filePath);
$startKey = array();
$stopKey = array();
if($_POST['startY'] == ""){
$startY = 0;
}//if startY
else{
$startY = intval($_POST['startY']);
}//else
if($_POST['stopY'] == ""){
$stopY = 0;
}//if stopY
else{
$stopY = intval($_POST['stopY']);
}//else
//store an array of start keys and
//stop keys. basically the startkey
//array has all of the keys of content
//where we need to start stored
//the same for the stopkey array
//except with the stop keys
foreach($content as $key => $i){
if((strpos($i, $startText)) !== false){
array_push($startKey, ($key + $startY));
}//if start text
if(strpos($i, $stopText) !== false){
array_push($stopKey, ($key + $stopY));
}//if
}//foreach
$fileCounter = 0;
//run everytime there is a start key position
//in the $contents array
foreach($startKey as $key2 => $j){
//this is the temp filename with the counter
$tempFileName = $filePath.$fileCounter;
//if the value of the start key is less
//than the value of the stop key then we need to write
//a file
if($j <= $stopKey[$fileCounter]){
//if the file has NOT been created
//we will run other wise the file
//exists meaning there were two
//start texts before the stop text
//this is how I get around
//doulbe start texts w/o creating
//extra files
if(!(is_file($tempFileName)) ){
for($temp = $j; $temp <= $stopKey[$fileCounter]; $temp++){
createFile($filePath, $content[$temp], $fileCounter);
}//while
}//if !
//else we need to increment the file name
else{
$fileCounter++;
}//else
}//if $j
}//foreach
}//startStop
echo("<p class = 'text'>$fileCount file(s) created.</p>");
function createFile($path, $data, $counter){
$newFile = ($path.$counter);
$f = fopen($newFile, 'a');
if(fwrite($f, $data));
else{
echo("<p class = 'text'>There was an error in writing the file: ".getFileName($newFile).". The file was not written.");
die();
}//else
}//createFile