book = simplexml_load_file ($file); $this->title_info(); } function title_info (){ $title_info = 'title-info'; include ('ru-ru.php'); foreach ($this->book->description->$title_info->children() as $key=>$children){ switch ($key){ case 'genre': $this->book_info['genre'][] = $genre_table_ru[(string) $children]; break; case 'book-title': $this->book_info['book-title'] = $children; break; case 'author': //Автор(ы) произведения $last_name = 'last-name'; $first_name = 'first-name'; $middle_name = 'middle-name'; $home_page = 'home-page'; $this->book_info['author'][] = array ( 'first-name'=>$children->$first_name, 'middle-name'=>$children->$middle_name, 'last-name'=>$children->$last_name, 'home-page'=>$children->$home_page, 'email'=>$children->email); break; case 'annotation': $this->book_info['annotation'] = $children->asXML(); break; case 'coverpage': $this->book_info['coverpage'] = $children->image; break; case 'date': // хранит дату создания документа. $this->book_info['date'] = $children; break; case 'translator': //Переводчик(и) $last_name = 'last-name'; $first_name = 'first-name'; $middle_name = 'middle-name'; $home_page = 'home-page'; $this->book_info['translator'][] = array ( 'first-name'=>$children->$first_name, 'middle-name'=>$children->$middle_name, 'last-name'=>$children->$last_name, 'home-page'=>$children->$home_page, 'email'=>$children->email); break; case 'lang': //Язык книги $this->book_info['lang'] = $children; break; case 'year': // год издания книги. $this->book_info['year'] = $children; break; } } $this->table_of_content = $this->table_of_content(); } function print_image ($image){ //print_r($image); $image = $image->asXML(); preg_match("/[\"|\']\#([\S]+\.[\S]+)[\"|\']/i", $image, $image_name); $image_name = $image_name[1]; foreach ($this->book->binary as $binary){ if ($binary[id] == $image_name){ $image = base64_decode($binary); $file = fopen($image_name, 'w'); fwrite($file, $image); fclose($file); echo ''; } } } function table_of_content($book='false'){ //Надо было как-то отметить, что в начале читать с самого начала , при этом не задавая это явно в методе. if (!empty($this->table_of_content)){return $this->table_of_content;} //Проверка на наличие if ($book == 'false'){$book = $this->book->body;} foreach ($book as $key=>$body){ if (isset($body->title)){ $title = $this->__clear_string($body->title->asXML()); if (!empty($title)){ //echo $title.' '; $title_array[] = $title; } } $title_array_temp = $this->table_of_content($body->section); if (!empty($title_array_temp)){$title_array[] = $title_array_temp;} } return $title_array; } function content($book='false', $i=-1){ if ($book == 'false'){$book = $this->book->body;} ++$i; foreach ($book->children() as $key=>$body){ switch ($key){ case 'image': $this->print_image ($body); break; case 'title': if ($i == 0){$i=1;} $title = $this->__clear_string($body->asXML()); echo ''.$title.''; break; case 'section': $this->content($body, $i); break; case 'epigraph': echo ' '.$this->__clear_string($body->asXML()).' '; break; default: echo $this->__clear_string($body->asXML(), false); } } } function print_table_of_content($table = false){ if (!$table){$table = $this->table_of_content; } echo ' '; foreach ($table as $row){ if (is_array($row)){ $this->print_table_of_content($row); }else{ echo ' '.$row.' '; } } echo ' '; } private function __clear_string ($string, $p = true){ $string = preg_replace('/\|\<\/title\>/im', '', $string); //|\|\<\/p\> $string = preg_replace('/\s+/m', " ", $string); $string = preg_replace('/^\s+|\s+$/', '', $string); if ($p){$string = preg_replace('/\|\<\/p\>/im', '', $string);} return $string; } } $book = new fictionbook; $book->book_load("12.fb2"); //echo ' '; $book->print_table_of_content(); $book->content(); ?>