Skip to content

Instantly share code, notes, and snippets.

@Jaimies
Forked from NewEXE/parser.php
Last active September 24, 2019 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jaimies/67fa446ce70390706fbbb7619fcac7e9 to your computer and use it in GitHub Desktop.
Save Jaimies/67fa446ce70390706fbbb7619fcac7e9 to your computer and use it in GitHub Desktop.
LoveRead.ec parser (PHP)
<?php
/**
* Book ID on loveread.ec
* For example, for
* http://loveread.ec/read_book.php?id=2555&p=1
* ID is 2555.
*
* @param int $id
*/
public function getFromLoveread(int $id) {
$htmlFirstPage = iconv('windows-1251', 'utf-8', file_get_contents("http://loveread.ec/read_book.php?id=$id&p=1"));
@preg_match("~&#8230;<a href='read_book.php\?id=$id&p=[0-9]+~", $htmlFirstPage, $pagesCount);
$pagesCount = ltrim(substr($pagesCount[0], -3), '=');
if(! is_numeric($pagesCount) || empty($pagesCount)) {
$pagesCount = ltrim(substr($pagesCount[0], -4), '=');
}
if($pagesCount) {
$patterns = array('~(\<(/?[^>]+)>)~is', '~&#769;~', '~&#039;~', '~&#252;~');
/* Finding the page title tag */
preg_match('/<title>(.*?)<\/title>/', $htmlFirstPage, $output);
$pagetitle = $output[1];
/* Removing LoveRead.ec message */
$booktitle = str_replace(' | LoveRead.ec - читать книги онлайн бесплатно', '', $pagetitle);
/* For some reason, the '|' symbol is not treated correctly when downloading the file, so replacing it with '-' */
$booktitle = str_replace('|', '-', $booktitle);
/* Finally, downloading the book with its title */
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $booktitle . '.txt"');
for($p = 1; $p <= $pagesCount; $p++) {
$url = "http://loveread.ec/read_book.php?id=$id&p=$p";
$html = file_get_contents($url);
$html = iconv('windows-1251', 'utf-8', $html);
preg_match('~<p.*class=MsoNormal>(.*?)</p>~is', $html, $matches);
$content = preg_replace($patterns, '', $matches[0]);
echo $content;
}
exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment