Advertisement
Guest User

function postrev_wordrepeat

a guest
Dec 6th, 2011
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.79 KB | None | 0 0
  1. <?
  2. function postrev_wordrepeat(){
  3.     global $db, $db_prefix , $config_data, $dir;
  4.     include($dir."language/".$config_data["lang"].".php");
  5.  
  6.  
  7.  
  8.     $wordCounter=array();// contador de palabra-repeticion
  9.  
  10.     // traigo todo el texto de cada noticia más el año
  11.     $consulta_noticias = "select hometext, bodytext from".$db_prefix." posts where estado = 1;";
  12.  
  13.  
  14.     $resultado = $db->sql_query($consulta_noticias);
  15.    
  16.     while ($row = $db->sql_fetchrow($resultado))
  17.         {
  18.        
  19.         $bodytext = strip_tags($row[bodytext]);
  20.         $hometext = strip_tags($row[hometext]);
  21.         $text = $hometext . $bodytext; // acumulo los textos
  22.        
  23.         $text = strip_tags($text);//afuera html
  24.         $text = mb_strtolower($text,'UTF-8'); // a minusculas respetando utf-8
  25.         $text = str_replace (array('\r\n', '\n', '+'), ',', $text);// bajadas de línea
  26.         $text = str_replace (array('–','(',')',':','.','?','!','_','*','-', '¿', '|','¡'), '', $text);//caracteres no válidos
  27.         $text = str_replace (array(' ','.'), ',', $text);//comas
  28.        
  29.         $arrText=explode(",",$text);//array con todas las palabras del post
  30.             unset($text);
  31.             foreach ($arrText as $value)  {
  32.                 $value=trim($value);//saco espacios
  33.                 if ( strlen($value)>0 ) {//no menores a 1
  34.                     if (array_key_exists($value,$wordCounter)){//si existe agregamos uno a la clave existente
  35.                         $wordCounter[$value]=$wordCounter[$value]+1;
  36.                     }
  37.                     else $wordCounter[$value]=1;//crear la clave en el array
  38.                 }
  39.             }  
  40.         }
  41.  
  42.  
  43.     unset($arrText);
  44.  
  45.     uasort($wordCounter, 'cmp');//ordeno de mayor a menor
  46.  
  47.     $keywords="<ol>";
  48.     foreach($wordCounter as $key=>$value){
  49.         $keywords.="<li>".$key." => ".$value."</li>";
  50.     }
  51.     $keywords.="</ol>";
  52.     unset($wordCounter);
  53.  
  54.     echo $keywords;
  55. }
  56. function cmp($a, $b) {//orden descendente
  57.     if ($a == $b) return 0;
  58.     return ($a < $b) ? 1 : -1;
  59. }
  60. ?>
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement