Décodage des couleurs en PHP
#1
En travaillant sur un système d'affichage des logs eggdrop sur un site web, j'ai retravaillé mon système de parsing des couleurs IRC.
Il semble bien fonctionner avec les logs eggdrop et mIRC.
Code PHP :
<?php
class irctext {
   var 
$colors = array(
      
'#FFFFFF',              //0
      
'#000000',              //1
      
'#000080',              //2
      
'#00AA00',              //3
      
'#FF0000',              //4
      
'#AA0000',              //5
      
'#AA00AA',              //6
      
'#FF8040',              //7
      
'#FFFF00',              //8
      
'#00FF00',              //9
      
'#008080',              //10
      
'#00FFFF',              //11
      
'#0000FF',              //12
      
'#FF00FF',              //13
      
'#808080',              //14
      
'#C0C0C0', //15
   
);
   var 
$ifg = '#FFFFFF';
   var 
$ibg = '#000000';
   function 
__construct($text) {
      if (
$text=='') return;
      else 
$this->text = htmlentities($text);
      
$this->parseunderline();
      
$this->parsebold();
      
$this->parsecolors();
      
$this->parseitalic();
      
$this->parsereverse();
      
$this->parselink();
      return 
$this->text;
   }
   private function 
returncolorstyle ($fg,$bg) {
      
$tmp = 'color: '.$this->colors[intval($fg)];
      if (
$bg != "")
         
$tmp .= ' ; background: '.$this->colors[intval($bg)].';';
      return 
$tmp;
   }
   private function 
parseunderline() {
      
$this->text = preg_replace("/(\x1f)(.*?)(\\1|$)/",'<u>$2</u>',$this->text);
   }
   private function 
parsebold() {
      
$this->text = preg_replace("/(\x02)(.*?)(\\1|$)/",'<b>$2</b>',$this->text);
   }
   private function 
parselink() {
      
$this->text = preg_replace("/(https?:\/\/[\w.:\-\/#\?=\~]{2,})\b/", '<a href="\\1">$1</a>', $this->text);
      
$this->text = preg_replace("/(irc:\/\/[\w.:\-\/#\?=\~\/]{2,})\b/", '<a href="\\1">$1</a>', $this->text);
   }
   private function 
parsecolors() {
      
$tmp = preg_split('/\x03/', $this->text);
      if (
count($tmp)==1) { return; }
      
$line = array();
      foreach(
$tmp as $piece) {
         
$line[] = preg_replace_callback(
             
"/^(\d{1,2})(,(\d{1,2}))?(.*)$/",
             
'self::decolor',
             
$piece
        
);
      }
      
$this->text = implode('', $line);
   }
   private function 
parseitalic() {
      
$this->text = preg_replace("/(\x1d)(.*?)(\\1|$)/",'<i>$2</i>',$this->text);
   }
   private function 
parsereverse() {
      
$this->text = preg_replace("/(\x16)(.*?)(\\1|$)/", '<span style="color:'.$this->ifg.';background:'.$this->ibg.'">$2</span>', $this->text);
   }
   private function 
decolor($matches) {
      return 
"<span style=\"".$this->returncolorstyle($matches[1],$matches[3])."\">".$matches[4]."</span>";
   }
} 

Pour l'utiliser:
Code PHP :
<?php
$fi 
= fopen('./monfichier.log', 'r');
while (
$line = fgets($fi)) {
   
$newline = new irctext($line);
   echo 
$newline->text, '<br />', PHP_EOL;
}
fclose($fi); 

N'hésitez pas à le tester avec les logs de différents clients et me faire vos retours. Je travaille sur un parseur de log plus complet dont cette classe n'est qu'un élément.
Présent sur le Tchat francophone
Répondre


Messages dans ce sujet
Décodage des couleurs en PHP - par CrazyCat - 31/03/2017, 15:32

Atteindre :


Utilisateur(s) parcourant ce sujet : 1 visiteur(s)
Tchat 100% gratuit - Discutez en toute liberté - Transitionnez vers Linux - Discussions dans le Lauragais - Communauté LGBT respectueuse - soutien aux victimes de cybermalveillance - pour les filles par les filles Le forum pour passer le temps