<?php

function pagehtml($html, $page, $limit){

    if ($page == 1){
        $start = $limit;
        $rstart = 0;
    }    
    else {
        $prevpage = $page - 1;
        $start = $prevpage * $limit;
        $r_start = endparagraf($html, $start);
    }
    
    $endpos = $page * $limit;
    $r_end = endparagraf($html, $endpos); 
    $len = $r_end - $r_start;

    return substr($html, $r_start, $len);
        
}



function endparagraf($html, $pos){

    $end = strpos($html, '</p>', $pos);

    if ($end)
        return $end;
    else
        return false;

}




//удаляем xtml
function convert2html($xfile){

    if (!file_exists($xfile)){
        echo 'file not found';
        return '';
    }

    $xhtml = file_get_contents($xfile);

    $result = str_replace('</body>', '', $xhtml);
    $result = str_replace('</html>', '', $xhtml);

    //$start_style =(strpos('<style>', $result))

    if ($pos = strpos('<body', $result)){
        $result = substr($result, $pos-1);
    }

    return $result;

}