View file news/classes/ReplaceText.php

File size: 3.14Kb
<?php

class ReplaceText
{
	function __construct($smiles_path)
	{
		$this->smiles_path = $smiles_path;
	}
	

	function link_parser($link_info)
	{
		if(!$link_info[3])
		{
			return '<a target="_blank" href="'.$link_info[1].'">'.$link_info[2].'</a>';
		}
		else
		{
			return '<a target="_blank" href="'.$link_info[3].'">'.$link_info[3].'</a>';
		}
	}

	function highlight($code) 
	{ 
		$code = strtr($code, array (
        '<br />' => '',
        '\\' => 'slash'
		));
		$code = html_entity_decode($code, ENT_QUOTES, 'UTF-8');
		$code = stripslashes($code); 
		if(!strpos($code,"<?") && substr($code,0,2)!="<?") {
			$code="<?php\n".trim($code)."\n?>"; 
		}  
		$code = trim($code); 
		$code = highlight_string($code,true);
		$code = strtr($code, array (
        'slash' => '&#92;',
        ':' => '&#58;',
        '[' => '&#91;',
        '&nbsp;' => ' '
		));
		return '<div class="code">'.$code.'</div>';
	} 
	
	public function detect_bbcode($text)
	{
		$text = preg_replace(array ('#\[code\](.*?)\[\/code\]#se'), array ("''.self::highlight('$1').''"), str_replace("]\n", "]", $text));
		$text = preg_replace('#\[b\](.*?)\[/b\]#si', '<b>\1</b>', $text);
		$text = preg_replace('#\[i\](.*?)\[/i\]#si', '<i>\1</i>', $text);
		$text = preg_replace('#\[u\](.*?)\[/u\]#si', '<u>\1</u>', $text);
		$text = preg_replace('#\[s\](.*?)\[/s\]#si', '<s>\1</s>', $text);
		$text = preg_replace('#\[center\](.*?)\[/center\]#si', '<center>\1</center>', $text);
		$text = preg_replace('#\[red\](.*?)\[/red\]#si', '<span style="color:red">\1</span>', $text);
		$text = preg_replace('#\[green\](.*?)\[/green\]#si', '<span style="color:green">\1</span>', $text);
		$text = preg_replace('#\[blue\](.*?)\[/blue\]#si', '<span style="color:blue">\1</span>', $text);
		$text = preg_replace_callback("~\\[url=(https?://.+?)\\](.+?)\\[/url\\]|(https?://([a-zA-Zа-яА-Я0-9\.\/\-\#\?\_\!\=&;]*))~", array('ReplaceText', 'link_parser'), $text);
		
		return $text;
	}
	
	function detect_smiles($text)
	{
		$text = strtr($text, array(
									':)' => '<img src="'.$this->smiles_path.'/happy.png" alt="smile" />',
									':(' => '<img src="'.$this->smiles_path.'/disappointed.png" alt="smile" />',
									':D' => '<img src="'.$this->smiles_path.'/excited.png" alt="smile" />',
									';)' => '<img src="'.$this->smiles_path.'/wink.png" alt="smile" />',
									':%' => '<img src="'.$this->smiles_path.'/silly.png" alt="smile" />',
									':bye:' => '<img src="'.$this->smiles_path.'/bye.png" alt="smile" />',
									':cat:' => '<img src="'.$this->smiles_path.'/cat.png" alt="smile" />',
									':chicken:' => '<img src="'.$this->smiles_path.'/chicken.png" alt="smile" />',
									':smoke:' => '<img src="'.$this->smiles_path.'/cigarette.png" alt="smile" />',
									':coffee:' => '<img src="'.$this->smiles_path.'/coffee.png" alt="smile" />',
									':music:' => '<img src="'.$this->smiles_path.'/music.png" alt="smile" />',
									':sleep:' => '<img src="'.$this->smiles_path.'/sleeping.png" alt="smile" />'
								));
		return $text;
	}
	
	function print_news($text)
	{
		return nl2br($this->detect_smiles($this->detect_bbcode($text)));
	}
	
	function print_date($date)
	{
		return date('d.m.Y в H:i', $date);
	}
}