<?

function korotko ($str, $dlina='700') {
if (preg_match('#^([\s\S]{'.$dlina.'}[^\!\.\?]*)[\.\!\?]#i',strip_tags($str),$match)) $code = $match[1];
else if (preg_match('#^([\s\S]{'.$dlina.'}[^\ \&nbsp;]*)[\ \&nbsp;]#i',strip_tags($str),$match)) $code = $match[1];
return $code; 
    }
    
    
if(!class_exists("multitpl"))
{
        class multitpl
        {
                var $tpls=array();
                var $ptpls=array(); //parsed templates
                var $wtpls=array(); //while templates (blocks, used many times)
                var $pwtpls=array(); //parsed while templates
                //**
                function file2string($file)
                {
                        if(file_exists($file) && is_readable($file))
                        {
                                $f=fopen($file,"rb");
                                $filecontent=fread($f,filesize($file));
                                fclose($f);
                                return $filecontent;
                        }else
                        {
                                return false;
                        }
                }
                //**
                function loadtpl($tpl)
                {
                        if($result=$this->file2string($tpl))
                        {
                                $this->tpls[$tpl]=preg_replace_callback('/\<script language="php"\>(.*)\<\/script\>/isU',create_function('$data','return eval($data[1]);'),$result);
                                return true;
                        }
                        return false;
                }
                //**
                function parse($rarray,$tpl) //replace array ( array('title' => 'Welcome', 'body' => 'This is an example',) )
                {
                        if(!isset($this->tpls[$tpl]) or !is_array($rarray)) return false;
                        //**
                        $tpltxt/*template text*/=$this->tpls[$tpl];
                        //**
                        foreach($rarray as $key=>$value)
                        {
                                if(!is_array($value)) $tpltxt=str_replace('{'.$key.'}', $value, $tpltxt);
                        }
                        //**
                        $this->ptpls[$tpl]=$tpltxt;
                        return true;
                }
                //**
                function getparsedtext($tpl)
                {
                        if(!isset($this->ptpls[$tpl])) return false;
                        //**
                        return $this->ptpls[$tpl];
                }
                //**
                function loadwtpl($tpl,$delimiter="<!--delimiter-->")
                {
                        if($result=$this->file2string($tpl))
                        {
                                $this->wtpls[$tpl]=explode($delimiter, preg_replace_callback('/\<php\>(.*)\<\/php\>/isU',create_function('$data','return eval($data[1]);'),$result));
                                return true;
                        }
                        return false;
                }
                //**
                function wparse($rarray,$tpl,$n) //replace array ( array('title' => 'Welcome', 'body' => 'This is an example',) )
                {
                        if(!isset($this->wtpls[$tpl][$n]) or !is_array($rarray)) return false;
                        //**
                        $tpltxt/*template text*/=$this->wtpls[$tpl][$n];
                        //**
                        foreach($rarray as $key=>$value)
                        {
                                if(!is_array($value)) $tpltxt=str_replace('{'.$key.'}', $value, $tpltxt);
                        }
                        //**
                        $this->pwtpls[$tpl][$n]=$tpltxt;
                        return true;
                }
                //**
                function wparseall($rarray,$tpl)
                {
                        if(!isset($this->wtpls[$tpl]) or !is_array($rarray)) return false;
                        //**
                        foreach($rarray as $key=>$value)
                        {
                                $this->wtpls[$tpl]=str_replace('{'.$key.'}', $value, $this->wtpls[$tpl]);
                        }
                        //**
                        return true;
                }
                //**
                function getwparsedtext($tpl,$n)
                {
                        if(!isset($this->pwtpls[$tpl][$n])) return false;
                        //**
                        return $this->pwtpls[$tpl][$n];
                }
                //**
                function fastparse($tpl,$rarray=array(),$echo=true)
                {
                        if(!isset($this->tpls[$tpl])) $this->loadtpl($tpl);
                        $this->parse($rarray,$tpl);
                        if($echo) echo $this->getparsedtext($tpl);
                        else return $this->getparsedtext($tpl);
                }
                //**
                function fastwparse($tpl,$rarray=array(),$num=0,$echo=true)
                {
                        if(!isset($this->wtpls[$tpl])) $this->loadwtpl($tpl);
                        $this->wparse($rarray,$tpl,$num);
                        if($echo) echo $this->getwparsedtext($tpl,$num);
                        else return $this->getwparsedtext($tpl,$num);
                }
        }
}
?>