1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
class bbcode{
/**
* @static
* @var $default_color
*/
private static $default_color = array(
'red','green','blue','yellow','purple','olive'
);
/**
* Retourne les couleurs à remplacer
* @param array $new_color
* @throws Exception
* @return string
*/
private function colorString(array $new_color=null){
if($new_color != null){
if(is_array($new_color)){
$tabs = array_merge((array) self::$default_color,(array) $new_color);
}else{
throw new Exception('new_color is not array');
}
}else{
$tabs = self::$default_color;
}
return implode('|', $tabs);
}
/**
* Les regex pour le remplacement des bbcode
* @param $new_color
*/
private function regPattern($new_color){
return array(
'/\[b](.+?)\[\/b\]/i','/\[i](.+?)\[\/i\]/i',
'/\[u](.+?)\[\/u\]/i',
'/\[color=('.self::colorString($new_color).'|#[[:xdigit:]]{6})\](.+?)\[\/color\]/i',
'/\[quote\](.+?)\[\/quote\]/i',
'/\[url=(.+?)\](.+?)\[\/url\]/i'
);
}
/**
* Les balises html qui remplace le bbcode pour l'affichage
* @param bool $clear
*/
private function strHtml($clear=false){
if($clear == true){
$pattern = array(
'$1','$1','$1','$2','$1','$2'
);
}else{
$pattern = array(
'<strong>$1</strong>',
'<span style="font-style:italic;">$1</span>',
'<span style="text-decoration:underline;">$1</span>',
'<span style="color:$1;">$2</span>',
'$1',
'$2'
);
}
return $pattern;
}
/**
* Fonction qui effectue la conversion du bbcode=>html
* @param string $string
* @param array $str_option
*/
public static function html_convert($string,$str_option=array('linebreaks'=>true,'clear'=>false,'new_color'=>null)){
if($string != null OR ($string != '')){
if($str_option['linebreaks']!=false){
$lb = nl2br($string);
}else{
$lb = $string;
}
return preg_replace(self::regPattern($str_option['new_color']), self::strHtml($str_option['clear']), $lb);
}else{
return $string;
}
}
} |
Partager