//Класс парсера keywords, description и постера
class MetaTags
{
private $string = '';
private $length = '';
public function __construct($string)
{
$this->string = $string;
}
private function cute_tags($string, $length)
{
$pattern = array('#\[img=(left|right)\].*?\[/img\]\s*#isu', '#\[img\].*?\[/img\]#isu', '#http://.*? #isu', '#\[spoiler=".+?"\].*?\[/spoiler\]\s*#isu', '#\[.*?\]#isu', '#&(.+?);#');
$replacement = array('', '', '', '', '', '');
$preg_replace = preg_replace($pattern, $replacement, $string);
$strip_tags = strip_tags($preg_replace);
$search = array(' ', '\"', '\n\n', '\n', ':', '(', ')');
$replace = array(' ', '', ' ', ' ', '', '', '');
$str_replace = str_replace($search, $replace, $strip_tags);
$mb_substr = mb_substr($str_replace, 0, $length, 'UTF-8');
$mb_strrpos = mb_strrpos($mb_substr, ' ', 'UTF-8');
$string = mb_substr($mb_substr, 0, $mb_strrpos, 'UTF-8');
return trim($string);
}
private function parser_poster()
{
preg_match_all('/\[img=(left|right|center)\](.*?)\[\/img\]/i', $this->string, $poster_align, PREG_SET_ORDER);
preg_match_all('/\[img\](.*?)\[\/img\]/i', $this->string, $poster, PREG_SET_ORDER);
if(isset($poster_align[0][2]))
{
$poster_img = $poster_align[0][2];
}
elseif(isset($poster[0][1]))
{
$poster_img = $poster[0][1];
}
else
{
$poster_img = false;
}
return $poster_img;
}
private function meta_keywords()
{
$str_replace = str_replace(array(',', '.'), '', $this->string);
$keywords = explode(' ', mb_strtolower($this->cute_tags($str_replace, 10000), 'UTF-8'));
foreach ($keywords as $k => $word)
{
if (strlen($word) < 7)
{
unset($keywords[$k]);
}
}
$keywords = array_slice($keywords, 0, 50);
$keywords = array_unique($keywords);
$keywords = array_rand(array_flip($keywords), 10);
$keywords = implode(', ', $keywords);
return $keywords;
}
private function meta_description()
{
if(preg_match_all('#\[desc\].*?\[/desc\]\s*#isu', $this->string, $descr))
{
foreach ($descr as $row)
{
$description = preg_replace("#\[desc\](.*?)\[/desc\]#si", "\\1", $row);
}
$description = implode(' ', $description);
}
elseif(preg_match_all('#\[b\]Описание(.*?)\[/b\](.*?)\[topiccut\]\s*#isu', $this->string, $descr))
{
foreach ($descr as $row)
{
$description = preg_replace("#\[b\]Описание(.*?)\[/b\](.*?)\[topiccut\]#si", "\\1", $row);
}
$description = implode(' ', $description);
}
else
{
$description = $this->string;
}
return $this->cute_tags($description, 250);
}
public function meta()
{
return DB()->escape(serialize(array('description' => $this->meta_description(), 'keywords' => $this->meta_keywords(), 'poster' => $this->parser_poster())));
}
}
// конец класса парсера keywords, description и постера