![Picture of Asbjorn Grandt Picture of Asbjorn Grandt](/graphics/unknown.gif)
Asbjorn Grandt - 2015-09-01 19:40:05 -
In reply to message 4 from Asbjorn Grandt
When you say EPubChapter.php I assume you mean EPubChapterSplitter.php
I just tested it on the current version of PHPePUB, and it detected my headers, and returned an array with the parts.
$splitter = new EPubChapterSplitter();
$searchString = '/<h1/i';
$html2 = $splitter->splitChapter($chapter5, true, $searchString);
// $html2 is an array where the keys are the entire line, including start and end tags of the hit.
// and the value is the segment for the match.
// The returned array can just be parsed to the addChapter like this:
// $book->addChapter($cName, "Chapter005.html", $html2, true);
// and EPub will add the parts automatically.
// However, often you'd want to try to get a measure of control over the process
$idx = 0;
while (list($k, $v) = each($html2)) {
$idx++;
// Because we used a string search in the splitter, the returned hits are put in the key part of the array.
// The entire HTML tag of the line matching the chapter search.
// Strip start and end tags. This Regexp will keep the tag name as well as the data between them.
preg_match('#^<(\w+)\s*.*?>(.+)</\s*\1>$#i', $k, $cName);
// This is simply to clean up the chapter name, it can't contain any HTML.
// Because of the back reference, the tag name is in $cName[1], and the content is in $cName[2], this is to be the chapter name in the TOC.
$cName = preg_replace('#<br.+?>#i', " - ", $cName[2]); // Change any line breaks in the chapter name to " - "
$cName = preg_replace('#<.+?>#i', " ", $cName); // Remove any other tags
$cName = preg_replace('#\s+#i', " ", $cName); // clean the chapter name by removing any double spaces left behind to single space.
$book->addChapter(trim($cName), "Chapter005_" . $idx . ".html", $v, true);
}