Для проекта, над которым я работаю сейчас (привет немцу
) нужно было автоматически генерировать Meta описание и ключевые слова к контенту, который также автоматически парсится из сайтов – доноров. Под эти нужны и был написан этот класс. Думаю комментарии излишни.
<?php
class Meta
{
private $_keyword_count = 20;
public function __construct( $keyword_count = false )
{
if( (int) $keyword_count ) $this->_keyword_count = $keyword_count;
}
public function setKeywordCount( $keyword_count )
{
if( (int) $keyword_count ) $this->_keyword_count = $keyword_count;
}
public function generateMeta( $text )
{
$newarr = array ();
$quotes = array ("\x22", "\x60", "\t", "\n", "\r", ",", ".", "/", "¬", "#", ";", ":", "@", "~", "[", "]", "{", "}", "=", "-", "+", ")", "(", "*", "^", "%", "$", "<", ">", "?", "!", '"');
$fastquotes = array ("\x22", "\x60", "\t", "\n", "\r", '"', "\\", '\r', '\n', "/", "{", "}", "[", "]" );
$text = str_replace( " ", " ", $text );
$text = str_replace( '<br />', ' ', $text );
$text = strip_tags( $text );
$text = preg_replace( "#&(.+?);#", "", $text );
$text = trim(str_replace( " ,", "", stripslashes( $text )));
$text = str_replace( $fastquotes, '', $text );
$text = str_replace( $quotes, ' ', $text );
$arr = explode( " ", $text );
foreach ( $arr as $word ) {
if( strlen( $word ) > 4 ) $newarr[] = $word;
}
$arr = array_count_values( $newarr );
arsort( $arr );
$arr = array_keys( $arr );
$total = count( $arr );
$offset = 0;
$arr = array_slice( $arr, $offset, $keyword_count );
$return['keywords'] = implode( ", ", $arr );
$return['description'] = substr( $text, 0, 190 );
return $return;
}
}