How do I split the content below into separate files without the placeholder tags. I'd also like to take the text inside the placeholder tags and place them inside a new contents file.
<div class='placeholder'>The First Chapter</div>
This is some text.
<div class='placeholder'>The Second Chapter</div>
This is some more text.
<div class='placeholder'>Last Chapter</div>
The last chapter.
Thanks.
UPDATE:
I've tried a modified version of MartinodF code, but can't get it to work.
$text=file_get_contents("t.txt");
$parts = preg_split('/\n?<div class=\'placeholder\'>(.+?)<\/div>\n/im', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
$parts_num = count($parts) / 2;
$titles = $files = array();
for($x = 0; $x < $parts_num - 1; $x++) {
$titles[] = $parts[$x * 2 + 1];
$files[] = $parts[$x * 2 + 1] . "\n" . $parts[$x * 2 + 2];
}
var_dump($titles);
var_dump($files);
echo $titles[1];
UPDATE 2: No longer reliant on separate txt file, but still doesn't work.
$text="<div class='placeholder'>The First Chapter</div>
This is some text.
<div class='placeholder'>The Second Chapter</div>
This is some more text.
<div class='placeholder'>Last Chapter</div>
The last chapter.
";
$parts = preg_split('/\n?<div class=\'placeholder\'>(.+?)<\/div>\n/im', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
$parts_num = count($parts) / 2;
$titles = $files = array();
for($x = 0; $x < $parts_num - 1; $x++) {
$titles[] = $parts[$x * 2 + 1];
$files[] = $parts[$x * 2 + 1] . "\n" . $parts[$x * 2 + 2];
}
var_dump($titles);
var_dump($files);
echo $titles[1];