All pastes #146726 Raw Edit

zCode parser

public php v1 · immutable
#146726 ·published 2006-08-23 20:40 UTC
rendered paste body
<?phpfunction parse_zcode($source) {	define('smilies_path', './images/smilies');		$str_pattern = array('&quot;', '&#039;');	$str_replace = array('"', '\'');	$source = str_replace($str_pattern, $str_replace, $source);		$pattern = array(		'`&lt;gras&gt;(.+)&lt;/gras&gt;`imsU',		'`&lt;italique&gt;(.+)&lt;/italique&gt;`imsU',		'`&lt;souligne&gt;(.+)&lt;/souligne&gt;`imsU',		'`&lt;barre&gt;(.+)&lt;/barre&gt;`imsU',		'`&lt;couleur\s*nom="(.+)"&gt;(.+)&lt;/couleur&gt;`imsU',		'`&lt;police\s*nom="(.+)"&gt;(.+)&lt;/police&gt;`imsU',		'`&lt;taille\s*valeur="(.+)"&gt;(.+)&lt;/taille&gt;`imsU',		'`&lt;titre1&gt;(.+)&lt;/titre1&gt;`imsU',		'`&lt;titre2&gt;(.+)&lt;/titre2&gt;`imsU',		'`&lt;acronyme&gt;(.+)&lt;/acronyme&gt;`imsU',		'`&lt;lien&gt;(.+)&lt;/lien&gt;`imsU',		'`&lt;lien\s*url="(.+)"&gt;(.+)&lt;/lien&gt;`imsU',		'`&lt;image&gt;(.+)&lt;/image&gt;`imsU',		'`&lt;attention&gt;(.+)&lt;/attention&gt;`imsU',		'`&lt;information&gt;(.+)&lt;/information&gt;`imsU',		'`&lt;question&gt;(.+)&lt;/question&gt;`imsU',		'`&lt;erreur&gt;(.+)&lt;/erreur&gt;`imsU',		'`&lt;citation&gt;(.+)&lt;/citation&gt;`imsU',		'`&lt;citation\s*nom="(.+)"&gt;(.+)&lt;/citation&gt;`imsU',		'`&lt;secret&gt;(.+)&lt;/secrect&gt;`imsU',		'`&lt;flottant\s*valeur="(.+)"&gt;(.+)&lt;/flottant&gt;`imsU',		'`&lt;position\s*valeur="(.+)"&gt;(.+)&lt;/position&gt;`imsU'	);	$replace = array(		'<strong>$1</strong>',		'<em>$1</em>',		'<em class="souligne">$1</em>',		'<span class="barre">$1</span>',		'<em class="couleur $1">$2</em>',		'<font face="$1">$2</font>',		'<span class="$1">$2</span>',		'<h3>$1</h3>',		'<h4>$1</h4>',		'<acronym title="acronyme">$1</acronym>',		'<a href="$1">$1</a>',		'<a href="$1">$2</a>',		'<img src="$1" />',		'<strong class="remarque_attention">$1</strong>',		'<strong class="remarque_information">$1</strong>',		'<strong class="remarque_question">$1</strong>',		'<strong class="remarque_erreur">$1</strong>',		'<span class="citation">Citation : </span><div class="citation2">$1</div>',		'<span class="citation">Citation ($1) : </span><div class="citation2">$2</div>',		'<span class="spoiler">Secret (cliquez pour afficher)</span><div class="spoiler2" onclick="switch_spoiler(this)"><div class="spoiler3">$1</div></div>',		'<div class="flot_$1">$2</div>',		'<div class="$1">$2</div>'	);	$source = preg_replace($pattern, $replace, $source);				function parse_code($matches) {		$types = array('html' => 'HTML', 'xhtml' => 'HTML', 'css' => 'CSS', 'javascript' => 'JavaScript',			'xml' => 'XML', 'php' => 'PHP', 'sql' => 'SQL', 'asp' => 'ASP',			'smarty'=> 'Smarty', 'apache' => 'Apache', 'perl' => 'Perl',			'actionscript' => 'ActionScript', 'c' => 'C', 'cpp' => 'C++',			'csharp' => 'C#', 'd' => 'D', 'java' => 'Java', 'python' => 'Python',			'pascal' => 'Pascal', 'delphi' => 'Delphi', 'vb' => 'VB',			'vbnet' => 'VB .Net', 'qbasic' => 'QBasic', 'asm' => 'Asm',			'darkbasic' => 'DarkBasic', 'ocaml' => 'Ocaml', 'ada' => 'Ada',			'mpasm' => 'Mpasm', 'nsis' => 'Nsis', 'visualfoxpro' => 'Visual FoxPro',			'objc' => 'Objc', 'matlab' => 'MatLab', 'diff' => 'Diff',			'oobas' => 'Oobas', 'oracle8' => 'Oracle8', 'vhdl' => 'Chdl',			'caddcl' => 'Caddcl', 'cadlisp' => 'Cadlisp', 'c_mac' => 'C_mac',			'lisp' => 'Lisp', 'bash' => 'Bash', 'lua' => 'Lua', 'zcode' => 'Zcode',			'' => 'Autre', 'console' => 'Console'		);				if (!array_key_exists($matches[1], $types)) {			$matches[1] = '';		}				$matches[2] = html_entity_decode($matches[2]);				$type = $types[$matches[1]];		require_once('./geshi.php');				$geshi = new GeSHi($matches[2], 'HTML');		$new_str = $geshi->parse_code();		$new_str = '<span class="code">Code '. $matches[1] .'</span><div class="code2 '. $matches[1] .'">La pr&eacute;visualisation du code ne fonctionne pas, d&eacute;sol&eacute;</div>';		return $new_str;	}		$source = preg_replace_callback('`&lt;code\s*type="(.+)"&gt;(.+)&lt;/code&gt;`imsU', 'parse_code', $source);		function parse_list($matches) {		$new_str = preg_replace('`(\r\n|\r|\n)*(&lt;puce&gt;.+&lt;/puce&gt;)(\r\n|\r|\n)*`imsU', '$2', $matches[3]);			if(preg_match('`&lt;liste\s*(type="(.?)")?&gt;((?:.(?!&lt;liste&gt;))*)&lt;/liste&gt;`imsU', $matches[3]) != 0) {			$new_str = preg_replace_callback('`&lt;liste\s*(type="(.?)")?&gt;((?:.(?!&lt;liste&gt;))*)&lt;/liste&gt;`imsU', 'parse_list', $new_str);		}			$new_str = preg_replace('`&lt;puce&gt;(.+)&lt;/puce&gt;`imsU', '<li>$1</li>', $new_str);			if(isset($matches[1]) && $matches[1] != '') {			$new_str = '<ol class="ol_'. $matches[2] .'">'. $new_str .'</ol>';		} else {			$new_str = '<ul>'. $new_str .'</ul>';		}				return $new_str;	}	$source = preg_replace_callback('`&lt;liste\s*(type="(.?)")?&gt;((?:.(?!&lt;liste&gt;))*)&lt;/liste&gt;`imsU', 'parse_list', $source);	$source = preg_replace_callback('`&lt;liste\s*(type="(.?)")?&gt;(.+)&lt;/liste&gt;`imsU', 'parse_list', $source);		function parse_table($matches) {		$new_str = preg_replace('`&lt;ligne&gt;(.+)&lt;/ligne&gt;`imsU', '<tr>$1</tr>', $matches[1]);		$new_str = preg_replace('`&lt;cellule&gt;(.+)&lt;/cellule&gt;`imsU', '<td>$1</td>', $new_str);		$new_str = preg_replace('`&lt;cellule\s*fusion_lig="(.+)"&gt;(.+)&lt;/cellule&gt;`imsU', '<td rowspan="$1">$2</td>', $new_str);		$new_str = preg_replace('`&lt;cellule\s*fusion_col="(.+)"&gt;(.+)&lt;/cellule&gt;`imsU', '<td colspan="$1">$2</td>', $new_str);				$new_str = '<table>'. $new_str .'</table>';				return $new_str;	}		$source = preg_replace_callback('`&lt;tableau&gt;(.+)&lt;/tableau&gt;`imsU', 'parse_table', $source);	$source = nl2br($source);		$source = str_replace('\'', '&#039;', $source);	$source = str_replace("\r\n", '', $source);	$source = str_replace("\n", '', $source);	$str_pattern = array(':)', ':D', ';)', ':p', ':lol:', ':euh:', ':(', ':o', ':colere', 'o_O', '^^', ':-°', ':magicien:', ':colere2:', ':diable:', ':ange:', ':ninja:', '>_<', ':pirate:', ':zorro:', ':honte:', ':soleil:', ':(', ':waw:');	$str_replace = array('<img src="'. smilies_path .'/smile.png" />', '<img src="'. smilies_path .'/heureux.png" />', '<img src="'. smilies_path .'/clin.png" />', '<img src="'. smilies_path .'/langue.png" />', '<img src="'. smilies_path .'/rire.gif" />', '<img src="'. smilies_path .'/unsure.gif" />', '<img src="'. smilies_path .'/triste.png" />', '<img src="'. smilies_path .'/huh.png" />', '<img src="'. smilies_path .'/mechant.png" />', '<img src="'. smilies_path .'/blink.gif" />', '<img src="'. smilies_path .'/hihi.png" />', '<img src="'. smilies_path .'/siffle.png" />', '<img src="'. smilies_path .'/magicien.png" />', '<img src="'. smilies_path .'/angry.gif" />', '<img src="'. smilies_path .'/diable.png" />', '<img src="'. smilies_path .'/ange.png" />', '<img src="'. smilies_path .'/ninja.png" />', '<img src="'. smilies_path .'/pinch.png" />', '<img src="'. smilies_path .'/pirate.png" />', '<img src="'. smilies_path .'/zorro.png" />', '<img src="'. smilies_path .'/rouge.png" />', '<img src="'. smilies_path .'/soleil.png" />', '<img src="'. smilies_path .'/pleure.png" />', '<img src="'. smilies_path .'/waw.png" />');	$source = str_replace($str_pattern, $str_replace, $source);	return $source;}