<?
function code($n)
{
$num=strlen($n);
switch ($num)
	{
	case 1: $a=0; $b=0; $c=0;$d=substr($n, 0, 1); break;
	case 2: $a=0; $b=0; $c=substr($n, 0, 1);$d=substr($n, 1, 1); break;
	case 3: $a=0; $b=substr($n, 0, 1); $c=substr($n, 1, 1);$d=substr($n, 2, 1); break;
	case 4: $a=substr($n, 0, 1); $b=substr($n, 1, 1); $c=substr($n, 2, 1);$d=substr($n, 3, 1); break;
	}
switch ($a)
	{
	case 1: $a1="одна тысяча"; break;
	case 2: $a1="две тысячи"; break;
	case 3: $a1="три тысячи"; break;
	case 4: $a1="четыре тысячи"; break;
	case 5: $a1="пять тысяч"; break;
	case 6: $a1="шесть тысяч"; break;
	case 7: $a1="семь тысяч"; break;
	case 8: $a1="восемь тысяч"; break;
	case 9: $a1="девять тысяч"; break;
	}
switch ($b)
	{
	case 1: $b1=" сто"; break;
	case 2: $b1=" двести"; break;
	case 3: $b1=" триста"; break;
	case 4: $b1=" четыреста"; break;
	case 5: $b1=" пятьсот"; break;
	case 6: $b1=" шестьсот"; break;
	case 7: $b1=" семьсот"; break;
	case 8: $b1=" восемьсот"; break;
	case 9: $b1=" девятьсот"; break;
	}
switch ($c)
	{
	case 2: $c1=" двадцать"; break;
	case 3: $c1=" тридцать"; break;
	case 4: $c1=" сорок"; break;
	case 5: $c1=" пятьдесят"; break;
	case 6: $c1=" шестьдесят"; break;
	case 7: $c1=" семьдесят"; break;
	case 8: $c1=" восемьдесят"; break;
	case 9: $c1=" девяносто"; break;
	}
if($c==1)
	{
	switch ($d)
  		{
		case 0: $c1=" десять"; break;
		case 1: $c1=" одиннадцать"; break;
		case 2: $c1=" двенадцать"; break;
		case 3: $c1=" тринадцать"; break;
		case 4: $c1=" четырнадцать"; break;
		case 5: $c1=" пятнадцать"; break;
		case 6: $c1=" шестнадцать"; break;
		case 7: $c1=" семнадцать"; break;
		case 8: $c1=" восемнадцать"; break;
		case 9: $c1=" девятнадцать"; break;
  		}
	$f=1;
	}
if($f==1)
$d1="";
else
	{
	switch ($d)
		{
		case 1: $d1=" один"; break;
		case 2: $d1=" два"; break;
		case 3: $d1=" три"; break;
		case 4: $d1=" четыре"; break;
		case 5: $d1=" пять"; break;
		case 6: $d1=" шесть"; break;
		case 7: $d1=" семь"; break;
		case 8: $d1=" восемь"; break;
		case 9: $d1=" девять"; break;
		}
	}
$e=$a1.$b1.$c1.$d1;
return $e;
}


function dir_tops($dir, $a)
{
$dir=trim($dir);
$arr=explode("/", $dir);
$cou=count($arr)-$a;
$d='';
$n=0;
foreach($arr as $ar)
	{
	$n++;
	if($n<$cou)
	$d .= $ar.'/';
	}
return $d;
}

function dir_top()
{
$dir=dirname("http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
$n=0;
while(strpos($dir, "/", $n)!==false)
	{
	$pos=strpos($dir, "/", $n);
	$n=$pos+1;
	}
$dir=substr($dir, 0, $n);
return $dir;
}
//////////////////////

function dir_tek()
{
return dirname("http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'])."/";
}


function doman_name($dom)
{
mb_internal_encoding("UTF-8");
$dom=trim($dom);
if (mb_strpos ($dom, "https:")!== false)
$pos=mb_strpos ($dom, "https:")+8; 
elseif(mb_strpos ($dom, "http:")!== false)
$pos=mb_strpos ($dom, "http:")+7; 
else 
$pos=0;
$dom=mb_substr($dom, $pos, mb_strlen($dom)-$pos); 
$dom_arr=explode("/", $dom);
$dom=$dom_arr[0];
$dom1=$dom;
if (mb_strpos ($dom1, "www.")=== 0) $dom1=mb_substr($dom1, 4, mb_strlen($dom1)-4); 
$arr=array(0=>$dom, 1=>$dom1);   
return $arr;
}

//////////////////////////
function type_img($type)
{
if (strpos($type, 'png') !== false) return '.png';
if (strpos($type, 'jpeg') !== false) return '.jpg';
if (strpos($type, 'gif') !== false) return '.gif';
}
//////////////////////////
function smalling($filename, $smallimage, $size, $typ, $ext, $vod_typ=0, $vod_text="", $lok="", $koef=0)
{
$size_img = getimagesize($filename);

if($vod_typ==6)
	{
	if($size_img[0]>$size_img[1])
	$vod_typ=3;
	else
	$vod_typ=2;
	}

switch($typ)
	{
	case 1: $u=max($size_img[0], $size_img[1])/$size; $g=0; break;
	case 2: $u=$size_img[0]/$size;  $g=0; break;
	case 3: $u=$size_img[1]/$size;  $g=0; break;
	case 4: $u=max($size_img[0], $size_img[1])/$size; 
	$u2=min($size_img[0], $size_img[1])/$size; break;
	}
	
if($u<1) $u=1;

	$w=$size_img[0]/$u;
	$h=$size_img[1]/$u;
	$ww=$w;
	$hh=$h;
	if($typ==4)
		{
		$u1=max(1,$u2);
		$w=$size_img[0]/$u1;
		$h=$size_img[1]/$u1;
		$ww=min($w, $h);
		$hh=$ww;
		if($w>$h) $g=($h-$w)/2; else $g=0;  
		}
		
    $dest_img = imagecreatetruecolor($ww, $hh);  
	
	switch($ext)
		{
		case ".jpg":
		$src_img = @imagecreatefromjpeg($filename); 
		break;
		
		case ".png":
		$src_img = imagecreatefrompng($filename);  
		break;
		
		case ".gif":
		$src_img = imagecreatefromgif($filename);  
		}
		
	if(!$src_img) return false;
    imagecopyresampled($dest_img, $src_img, $g, 0, 0, 0, $w, $h, $size_img[0], $size_img[1]);
	imagedestroy($src_img);
	
	if($vod_typ)
		{
		$r = 255;
		$g = 255;
		$b = 255;
		
		$kff=0.9;
		$vod_text = "  ".$vod_text."  ";

		if($vod_typ == 1)
			{
			$angle = -rad2deg(atan2((-$h),($w))); 
			$size = $kff*(($w+$h)/2)*2/strlen($vod_text);
	
			$box  = imagettfbbox ( $size, $angle, $lok, $vod_text );
			$x = $w/2 - abs($box[4] - $box[0])/2;
			$y = $h/2 + abs($box[5] - $box[1])/2;
			}
	
		elseif($vod_typ == 2)
			{
			$angle = 0; 
			$size = $kff*$w/strlen($vod_text);
	
			$box  = imagettfbbox ( $size, $angle, $lok, $vod_text );
			$x = $w/2 - abs($box[4] - $box[0])/2;
			$y = $h-15;
			}
	
		elseif($vod_typ == 3)
			{
			$angle = 90; 
			$size = $kff*$h/strlen($vod_text);
	
			$box  = imagettfbbox ( $size, $angle, $lok, $vod_text );
			$x = $w-15;
			$y = $h/2 + abs($box[5] - $box[1])/2;
			}
 
		elseif($vod_typ == 4)
			{
			$angle = 0; 
			$size = $kff*$w/strlen($vod_text);
	
			$box  = imagettfbbox ( $size, $angle, $lok, $vod_text );
			$x = 0;
			$y = $h-15;
			}
 
		elseif($vod_typ == 5)
			{
			$angle = 0; 
			$size = $kff*$w/strlen($vod_text);
	
			$box  = imagettfbbox ( $size, $angle, $lok, $vod_text );
			$x = $w - ($box[2] - $box[0]);
			$y = $h-15;
			}
 
		$c = imagecolorallocatealpha($dest_img, $r, $g, $b, $koef);
 
		imagettftext($dest_img, $size, $angle, $x, $y, $c, $lok, $vod_text);
		
		}
		
	imagejpeg($dest_img, $smallimage);
	imagedestroy($dest_img);
	return true;
}  

//////////////////////////
function reg_first($a)
{
mb_internal_encoding("UTF-8");
$b[0]=mb_strtolower($a);
$b[1]=mb_strtoupper(mb_substr($a, 0, 1)).mb_strtolower(mb_substr($a, 1, mb_strlen($a)-1));
$b[2]=mb_strtoupper($a);
return $b;
}

function search2 ($sea, $fild1)
{
mb_internal_encoding("UTF-8");
$sea = str_replace('&quot;', ' ', $sea);
$sea = str_replace('.', ' ', $sea);
$sea = str_replace(',', ' ', $sea);
$sea = str_replace(';', ' ', $sea);
$sea = str_replace(':', ' ', $sea);
$sea = str_replace("'", ' ', $sea);
$sea = str_replace('!', ' ', $sea);
$sea = preg_replace("/ +/", " ", $sea);

$sea=mb_strtolower($sea);
$okp = array("ая", "яя", "ый", "ий", "им", "ым", "ом", "ам", "их", "ых", "ов", "ой", "ое", "ее", "ие", "ые", "ки", "ка", "ок", "ек");
$oks = array("а", "ы", "е", "и", "я", "ь", "ю", "у", "о");
$okd = array("для", "к", "за", "от", "на", "в", "из", "у", "по", "под", "и");

$tit_arr=explode(" ", $sea);

foreach ($tit_arr as $t_arr)
{
if (!in_array($t_arr, $okd) && $t_arr!="") 
	{
	$o = mb_substr($t_arr, mb_strlen ($t_arr)-2, 2);
		if (in_array($o, $okp) && mb_strlen ($t_arr)>3) 
		$o1 = mb_substr($t_arr, 0, mb_strlen($t_arr)-2);
		else
		{
		$o = mb_substr($t_arr, mb_strlen ($t_arr)-1, 1);
  			if (in_array($o, $oks) && mb_strlen ($t_arr)>2) $o1 = mb_substr($t_arr, 0, mb_strlen($t_arr)-1);
  			else $o1=$t_arr;
		} 
	$o2=reg_first($o1);
	$trr[]=$o2;
	
	}
}

$a="";
$i=0;
if($trr)
	{
	foreach ($trr as $t_arr)
  		{
		if($i)
		$a .= " AND ";
		$a .= " (".$fild1." REGEXP '[[:<:]]".$t_arr[0]."' OR ".$fild1." REGEXP '[[:<:]]".$t_arr[1]."' OR ".$fild1." REGEXP '[[:<:]]".$t_arr[2]."')";
  		$i++;
		}
	}
  
return $a;
}
/////////////////////////
function aut ()
{
echo ' autor: aikuvshinov@mail.ru';
}
//////////
function obrez ($a, $kol)
{
mb_internal_encoding("UTF-8");
if (mb_strlen($a)>$kol)
$b=mb_substr($a, 0, $kol).' ...';
else
$b=$a;
return $b;
}

function col_znak ($a)
{
mb_internal_encoding("UTF-8");
$b=mb_strlen($a);
return $b;
}
/////////////
function _email($email)
{
if (!eregi("^([0-9a-z]([.]*[0-9a-z_-][.]*)*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-wyz][a-z](fo|g|l|d|m|mes|o|op|pa|ro|seum|t|u|v|z)?)$", $email) )
return false;
else
return true;
}
//////////////
function _url($url)
{
if (!eregi("^((http://)?[0-9a-z]([-.]?[0-9a-z])*\\.[a-wyz][a-z](fo|g|l|d|m|mes|o|op|pa|ro|seum|t|u|v|z)?[/]?)$", $url) )
return false;
else
return true;
}
//////////////
function links($msg)
{
echo "<p class=msg><b>".$msg."</b></p>";
echo "<p class=msg><a class=link href=# onClick='history.back()'>Back</a></p>";
exit();
}
///////////////////
function anti($a)
{
$a=str_replace("'", "", $a);
$a=str_replace("`", "", $a);
$a=str_replace("!", "", $a);
$a=strip_tags($a);
$a=stripslashes($a);

return $a;
}
/////////////////
function view_dir ($directory)
{
mb_internal_encoding("UTF-8");

$dir = opendir($directory);
while(($file = readdir($dir)))
	{
	if ((is_dir ($directory."/".$file)) && ($file != ".") && ($file != ".."))
		{
		$arr[]=$file;
		}
	}
closedir ($dir);
return $arr;
}
//////////////////
function _rec ($i_str, $id_mesto, $stolb=1)
{
global $arr_rec_blok;
global $mesto;

$im=$i_str.'_'.$id_mesto;
if($mesto[$im] && count($mesto[$im])>0)
	{
	shuffle($mesto[$im]);
		$rez = '<table valign=top cellSpacing=5 cellPadding=5 width=100%>';
		$i=0;
		foreach($mesto[$im] as $in)
		{
		
		if($i>=$stolb) {$i=0; $rez .= '<tr>';}
		
		if($arr_rec_blok[$in]['typ_rec']==1)
			{
			$rez .= '<td valign=top class=tbl bgcolor="#FFFBF0">'.$arr_rec_blok[$in]['content_rec'].'</td>';
			}
		
		elseif($arr_rec_blok[$in]['typ_rec']==2 )
			{
			$rez .= '<td align=center valign=top >'.$arr_rec_blok[$in]['content_rec'].'</td>';
			}
		elseif($arr_rec_blok[$in]['typ_rec']==3)
			{
			$wh=explode("_",$arr_rec_blok[$in]['banner']);
			$rez .=  '<td valign=top align=center>
			<div class="fl_hidden">
			<script type="text/javascript">swfobject.embedSWF("'.$arr_rec_blok[$in]['content_rec'].'", "Banner'.$arr_rec_blok[$in]['id_rec'].'", "'.$wh[0].'", "'.$wh[1].'", "9.0.0", "", false, "");</script>
			<div id="Banner'.$arr_rec_blok[$in]['id_rec'].'" ></div>
			</div>
			</td>';
			}
		$i++;
		//if($id_mesto==1) break;
		//if($id_mesto==2 && $i==3) break;
		}
		
		$rez .= '</table>';
		echo $rez;
	}
	
}
///////////////
function pages($jm, $total, $pn, $dop1='', $dop2='')
{
if ($total > 0) 
	{ 
	echo '<div class="wp-pagenavi">';
	if(!$pn) 
	{$jn=0; $jk=$jm-1;}
	else 
	{$jn=(ceil(($pn+1)/$jm)-1)*$jm; $jk=ceil(($pn+1)/$jm)*$jm-1; }
	
	if($jk > $total) $jk = $total;

	if($jn > 0) $back=' <a href ="'.$dop1.$dop2.($jn-1).'">...</a> ';
	if($jk < $total) $next=' <a href ="'.$dop1.$dop2.($jk+1).'">...</a> ';
	echo  $back;
	for ($i=$jn; $i <= $jk; $i++)
		{
		if($i==$pn)
			echo ' <span class="current">'.($i+1).'</span> ';
		else
			{
			if($i==0)
			echo ' <a href ="'.$dop1.'"> '.($i+1).'</a> ';
			else
			echo ' <a href ="'.$dop1.$dop2.$i.'"> '.($i+1).'</a> ';
			}
		}
	echo  $next;
	
	echo '</div>';
	}
}
/////////////////
function pages3($jm, $total, $pn, $dop='')
{
if ($total > 0) 
	{ 
	echo '<div class="wp-pagenavi">';
	if(!$pn) 
	{$jn=0; $jk=$jm-1;}
	else 
	{$jn=(ceil(($pn+1)/$jm)-1)*$jm; $jk=ceil(($pn+1)/$jm)*$jm-1; }
	
	if($jk > $total) $jk = $total;

	if($jn > 0) $back=' <a href ="?pn='.($jn-1).$dop.'">...</a> ';
	if($jk < $total) $next=' <a href ="?pn='.($jk+1).$dop.'">...</a> ';
	echo  $back;
	for ($i=$jn; $i <= $jk; $i++)
		{
			if($i==$pn)
			echo ' <span class="current">'.($i+1).'</span> ';
			else
			echo ' <a href ="?pn='.$i.$dop.'"> '.($i+1).'</a> ';
		}
	echo  $next;
	
	echo '</div>';
	}
}

//////////////
function pages4($jm, $total, $pn, $count, $sort, $sort2, $order)
{
if ($total > 0) 
	{ 
	echo '<div class="wp-pagenavi">';
	if(!$pn) 
	{$jn=0; $jk=$jm-1;}
	else 
	{$jn=(ceil(($pn+1)/$jm)-1)*$jm; $jk=ceil(($pn+1)/$jm)*$jm-1; }
	
	if($jk > $total) $jk = $total;

	if($jn > 0) $back=' <a onClick="sort_tab('.$count.', \''.$sort.'\', \''.$sort2.'\', '.($jn-1).', '.$order.')">...</a> ';
	if($jk < $total) $next=' <a onClick="sort_tab('.$count.', \''.$sort.'\', \''.$sort2.'\', '.($jk+1).', '.$order.')">...</a> ';
	echo  $back;
	for ($i=$jn; $i <= $jk; $i++)
		{
			if($i==$pn)
			echo ' <span class="current">'.($i+1).'</span> ';
			else
			echo ' <a onClick="sort_tab('.$count.', \''.$sort.'\', \''.$sort2.'\', '.$i.', '.$order.')"> '.($i+1).'</a> ';
		}
	echo  $next;
	
	echo '</div>';
	}
}

//////////////
function sort_num($tabl, $field_id, $field_num, $field_sort, $id, $per, $dop='')
{
if ($per==1) $per=-1; else $per=1;
$j=0;

$r = mysql_query ("SELECT ".$field_id." FROM ".$tabl.$dop."  ORDER BY ".$field_num.", ".$field_sort) or die(mysql_error());
$nr = mysql_num_rows($r);

if($nr)
	{
	while($rr = mysql_fetch_array($r))
		{
		$j++;
		$brr[$j]=$rr[$field_id];
		if ($rr[$field_id]==$id) $num=$j;
		}
	$now_num=$num+$per;
	if ($now_num<1) $now_num=1;
	if ($now_num>$nr) $now_num=$nr;

	foreach($brr as $index=>$br)
		{
 		if ($index!=$now_num && $index!=$num) $arr[$index]=$br;
 		elseif ($index==$now_num) $arr[$num]=$br;
 		elseif ($index==$num) $arr[$now_num]=$br;
		}
  
	foreach($arr as $ind=>$ar)
  		{
  		mysql_query("UPDATE ".$tabl." SET ".$field_num." ='".$ind."' WHERE ".$field_id."='".$ar."'"); 
  		}
	}
}
//////////////
function prew ($a)
{
$size_img = getimagesize($a);
$r=$size_img[0]/$size_img[1];
$g=70*$r;
$v=70;
echo '<img height='.$v.' width='.$g.' src="'.$a.'" border=0>';
}
////////////
function _font($a, $r=0, $f=0, $y=0)
{
if($a)	
	{
	switch ($r)
		{
		case 0: $b=number_format($a,$y,",",""); break;
		case 1: $b=number_format($a,$y,"."," "); break;
		case 2: $b=number_format($a,$y,",","."); break;
		case 3: $b=number_format($a,$y,".",","); break;
		case 4: $b=number_format($a,$y,".",""); break;
		}

	switch ($f)
		{
		case 0: $c=$b; break;
		case 1: $c="<font color=#FF0000>".$b."</font>"; break;
		case 2: $c="<font color=#660033>".$b."</font>"; break;
		case 3: $c="<font color=#009966>".$b."</font>"; break;
		case 4: $c="<font color=#0066CC>".$b."</font>"; break;
		}
	}
else $c='';
return $c;
}
/////////////////////////////////////////////////////
function video_add($a, $z=0)
{
$x=array(0,0,0);
$m=array("youtu.be", "youtube.com");

	$b=doman_name($a);
	$b=$b[1];
	
	if($b=="youtu.be")
		$d=explode("youtu.be/",$a);
		
	elseif($b=="youtube.com" && strpos($a, "youtube.com/watch", 0)!==false)
		{
		$q=parse_url($a);
		if($q['query'])
			{
			$u=explode("&",$q['query']);
			foreach ($u as $uu)
				{
				$z=explode("=",$uu);
				if($z[0]=='v') {$d[1]=$z[1]; break;}
				}
			} 
		}
	
	elseif($b=="youtube.com" && strpos($a, "youtube.com/embed/", 0)!==false)
		{
		$q=explode("youtube.com/embed/", $a);
		if($q[1])
			{
			$q2=explode('"', $q[1]);
			$d[1]=$q2[0];
			}
		}
		
	$e=trim($d[1]);
	
	if(!in_array($b, $m))
		$x[1]="- Неверно указана ссылка на видеоролик";
	elseif($e)
		{
		$x[2]=$e;
		$x[3]=$b;
		}
	else
		$x[1]="- Неверно указана ссылка на видеоролик";
		
return $x;	
}
////////////////
function video_youtub($a)
{
$y = '<iframe title="YouTube video player" width="600" height="400" src="http://www.youtube.com/embed/'.$a.'" frameborder="0" allowfullscreen></iframe>';
return $y;
}
///////////////////
function _intval($a, $b=0)
{
$a=str_replace(" ", "", $a);
$a=str_replace(",", ".", $a);
if(!$b)
	$c=intval($a);
else
	$c=floatval(number_format($a,$b,".",""));
return $c;
}
/////////////////
function _intval2($a,$b=1)
{
$a=str_replace(" ", "", $a);
$a=str_replace(",", ".", $a);
$c=floatval(number_format($a,$b,".",""));
$c=str_replace(",", ".", $c);
return $c;
}
//////////////////
function _pre ($a)
{
$a=strip_tags($a);
$a=str_replace("`","",$a);
$a=str_replace("'","",$a);
//$a=str_replace('"','',$a);
$a=htmlspecialchars($a);
return $a;
}
////////////////
function _pre_zag ($a)
{
mb_internal_encoding("UTF-8");
$a = preg_replace('/[^A-Za-z0-9а-яА-ЯёЁ,.-]/u', ' ', $a);
$a= preg_replace('((\D)(\.)(\D))', '$1'.'$2'.' '.'$3', $a);
$a= preg_replace('((\D)(\.)(\d))', '$1'.'$2'.' '.'$3', $a);
$a= preg_replace('((\d)(\.)(\D))', '$1'.'$2'.' '.'$3', $a);
$a= preg_replace('((\D)(,)(\D))', '$1'.'$2'.' '.'$3', $a);
$a= preg_replace('((\D)(,)(\d))', '$1'.'$2'.' '.'$3', $a);
$a= preg_replace('((\d)(,)(\D))', '$1'.'$2'.' '.'$3', $a);
$a=str_replace("  "," ",$a);
$a=str_replace("  "," ",$a);
$a=trim($a);
return $a;
}

//////////////////
function _pre2 ($a)
{
mb_internal_encoding("UTF-8");
$a=strip_tags($a);
$a=str_replace("`","",$a);
$a=str_replace("'","",$a);

$arr=explode("\n", $a);
$b="";

foreach ($arr as $t_arr)
	{
	if(trim($t_arr))
		{
		$b .= trim($t_arr)."<br>";
		$d=explode(" ", trim($t_arr));
		foreach($d as $dd)
			{
			if(mb_strlen($dd)>25) {$err = "Вы употребляете слишком длинные слова - ".$dd; break;}
			}
		}
	}

$a=$b;

$a=str_replace(";", "; ", $a);
$a=str_replace(":", ": ", $a);

$a= preg_replace('((\D)(\.)(\D))', '$1'.'$2'.' '.'$3', $a);
$a= preg_replace('((\D)(\.)(\d))', '$1'.'$2'.' '.'$3', $a);
$a= preg_replace('((\d)(\.)(\D))', '$1'.'$2'.' '.'$3', $a);
$a= preg_replace('((\D)(,)(\D))', '$1'.'$2'.' '.'$3', $a);
$a= preg_replace('((\D)(,)(\d))', '$1'.'$2'.' '.'$3', $a);
$a= preg_replace('((\d)(,)(\D))', '$1'.'$2'.' '.'$3', $a);
$a= preg_replace('((\))(\S))', '$1'.' '.'$2', $a);
$a= preg_replace('((\S)(\())', '$1'.' '.'$2', $a);

$a= preg_replace('((-+))', '-', $a);
$a= preg_replace('((\++))', '+', $a);
$a= preg_replace('((\=+))', '=', $a);
$a= preg_replace('((\*+))', '*', $a);

$a=str_replace('  ',' ',$a);
$a=str_replace('  ',' ',$a);
$a=str_replace(' )',')',$a);
$a=str_replace('( ','(',$a);
$a=str_replace(' .','.',$a);
$a=str_replace(' ,',',',$a);
$a=str_replace(' ;',';',$a);
$a=str_replace(' :',':',$a);

$a=str_replace("<br>", "\n", $a);
$a=trim($a);

if($err) 
	$rez[0]=$err;
else
	$rez[0]='';
$rez[1]=htmlspecialchars($a);
return $rez;
}
///////////////////
function _chis ($a, $b)
{
$a=str_replace(" ","",$a);
$a=str_replace(",",".",$a);
if($b=='intval')
$a=intval($a);
elseif($b=='floatval')
$a=floatval($a);
return $a;
}
/////////////////////
function full_del_dir ($directory)
{
$dir = opendir($directory);
while(($file = readdir($dir)))
	{
	if ( is_file ($directory."/".$file))
		{
		unlink ($directory."/".$file);
		}
	else if ( is_dir ($directory."/".$file) && ($file != ".") && ($file != ".."))
		{
		full_del_dir ($directory."/".$file);  
		}
	}
closedir ($dir);
rmdir ($directory);
return true;
}
///////////////////
function dat_rus($d, $a=0)
{
if(!$a || $a==2)
	{
	$mon[1]='января';
	$mon[2]='февраля';
	$mon[3]='марта';
	$mon[4]='апреля';
	$mon[5]='мая';
	$mon[6]='июня';
	$mon[7]='июля';
	$mon[8]='августа';
	$mon[9]='сентября';
	$mon[10]='октября';
	$mon[11]='ноября';
	$mon[12]='декабря';
	}
elseif($a==1)
	{
	$mon[1]='янв.';
	$mon[2]='фев.';
	$mon[3]='мар.';
	$mon[4]='апр.';
	$mon[5]='мая';
	$mon[6]='июня';
	$mon[7]='июля';
	$mon[8]='авг.';
	$mon[9]='сен.';
	$mon[10]='окт.';
	$mon[11]='ноя.';
	$mon[12]='дек.';
	}

$dt_d=date("j", strtotime($d));
$dt_m=date("n", strtotime($d));
$dt_y=date("Y", strtotime($d));

$dt=$dt_d." ".$mon[$dt_m];
if($a==2)
$dt .= ' '.$dt_y.' г.';
//$dt .= " ".$dt_y;
//$dt .= " ".date("H:i", strtotime($d));
return $dt;
}

function povtor($a, $field) {
mb_internal_encoding("UTF-8");
$a=mb_substr($a, 0, 150);
$b = " ".$field." LIKE '%%".$a."%%'  ";
return $b;
}
/*
function re_adres ($a) {
$a=urlencode(str_replace(' ','_',$a));
return $a;
}
*/
function cpu($string, $url_type=0) {
mb_internal_encoding("UTF-8");
$string = mb_strtolower($string);
$string = htmlspecialchars_decode(trim($string));

if(!$url_type)
	{
    $table = array(
                'а' => 'a',
                'б' => 'b',
                'в' => 'v',
                'г' => 'g',
                'д' => 'd',
                'е' => 'e',
                'ё' => 'yo',
                'ж' => 'zh',
                'з' => 'z',
                'и' => 'i',
                'й' => 'y',
                'к' => 'k',
                'л' => 'l',
                'м' => 'm',
                'н' => 'n',
                'о' => 'o',
                'п' => 'p',
                'р' => 'r',
                'с' => 's',
                'т' => 't',
                'у' => 'u',
                'ф' => 'f',
                'х' => 'h',
                'ц' => 'c',
                'ч' => 'ch',
                'ш' => 'sh',
                'щ' => 'csh',
                'ь' => '',
                'ы' => 'i',
                'ъ' => '',
                'э' => 'e',
                'ю' => 'yu',
                'я' => 'ya'
    			);

    $string = str_replace(array_keys($table), array_values($table),$string);
    $string = str_replace(array_keys($table), array_values($table),$string);
	$string = preg_replace('((\W))', ' ', $string);
	$string = trim($string);
	$string = str_replace(' ','_', $string);
	$string = str_replace('__','_', $string);
	$string = str_replace('__','_', $string);
	}
elseif ($url_type==1)
	{
	$string = preg_replace('/[^A-Za-z0-9а-яА-Я]/u', ' ', $string);
	$string = trim($string);
	$string = str_replace(' ','_', $string);
	$string = str_replace('__','_', $string);
	$string = str_replace('__','_', $string);
	$string = urlencode($string);
	}
return $string;
} 

function mail_utf8($to, $subject = '(No subject)', $message = '', $from) 
	{
  	$header = 'MIME-Version: 1.0'."\n".'Content-type: text/html; charset=UTF-8'."\n".'From: '.$from."\n";
  	if(@mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header))
	return true;
	else
	return false;
	}

function cifr($u, $adr)
{
$u=intval($u);
for ($i=0; $i<strlen($u); $i++)
{
$arr[]=substr($u, $i, 1);
}

$long=16*count($arr);
$img=imagecreate($long, 25);
imagecolorallocate($img, 255,255,255);

$gr[0]=array(250,198,112);
$gr[1]=array(238,117,244);
$gr[2]=array(154,137,224);
$gr[3]=array(118,243,152);
$gr[4]=array(128,64,0);
$gr[5]=array(85,80,43);
$gr[6]=array(12,125,18);
$gr[7]=array(210,210,0);
$gr[8]=array(0,0,128);

shuffle($gr);

foreach($gr as $g)
{
$green[]=imagecolorallocate($img, $g[0],$g[1],$g[2]);
}

foreach ($arr as $index=>$c)
	{
	$x=$index*16;
	if($c==0 || $c==2 || $c==3 || $c==5 || $c==7 || $c==8 || $c==9) imageline($img, $x+0,0,$x+12,0, $green[0]);
	if($c==3 || $c==4 || $c==5 || $c==6 || $c==8 || $c==9) imageline($img, $x+0,12,$x+12,12, $green[1]);
	if($c==0 || $c==2 || $c==5 || $c==6 || $c==8 ) imageline($img, $x+0,24,$x+12,24, $green[2]);

	if($c==3 || $c==6 || $c==7 ) imageline($img, $x+0,12,$x+12,0, $green[3]);
	if($c==2 || $c==3 || $c==9 ) imageline($img, $x+0,24,$x+12,12, $green[4]);

	if($c==0 || $c==4 || $c==5 || $c==8 || $c==9 ) imageline($img, $x+0,0,$x+0,12, $green[5]);
	if($c==0 || $c==6 || $c==7 || $c==8 ) imageline($img, $x+0,12,$x+0,24, $green[6]);
	if($c==0 || $c==1 || $c==2 || $c==4 || $c==8 || $c==9 ) imageline($img, $x+12,0,$x+12,12, $green[7]);
	if($c==0 || $c==1 || $c==4 || $c==5 || $c==6 || $c==8 ) imageline($img, $x+12,12,$x+12,24, $green[8]);
	}
imagegif($img, $adr);
imagedestroy($img);
}

?>