Scripts

Highlight HTML en XML

n.a.v. de highlight SQL functie heb ik nu ook een highlight HTML functie gebouwd. Op en aanmerkingen zijn wederom erg welkom! Enjoy!

highlight-html-en-xml
[code]
<?php

	$my_html_string = 'your html string';
	echo highlight_html($my_html_string);


	// Highlight HTML code
	function highlight_html($string, $bAddDefaultCss = false)
	{
		$result = '';

		if($bAddDefaultCss)
		{
			$result .= '<style type="text/css">'
			. 'code.html { background: #FCFCFC; border: #E8E8E8 solid 1px; color: #000000; display: block; overflow: auto; padding: 20px; white-space: nowrap; }'
			. 'code.html .tag { color: #0000FF; }'
			. 'code.html .comment { color: #008000; }'
			. 'code.html .attribute { color: #FF0000; }'
			. 'code.html .value { color: #FF00FF; }'
			. '</style>';
		}

		$expr = '/<!--((\n|.)*?)-->|<[a-z0-9]+(([\n\s]+)([a-z0-9\-]+)([\n\s]*)=([\n\s]*)"([^"]*)")*([\n\s]*\/)?>|<\/[a-z0-9]+>|(.)/i';
		$matches = array();

		preg_match_all($expr, $string, $matches);
		// print_r($matches[0]);
		$bAsValue = false;

		for($i = 0; $i < sizeof($matches[0]); $i++)
		{
			if(strcasecmp($match = $matches[0][$i], "") !== 0)
			{
				if(preg_match('/<!--((\n|.)*?)-->/i', $match)) // Comment
				{
					$result .= '<span class="comment">' . stringToHtml($match) . '</span>';
				}
				elseif(preg_match('/<\/[a-z0-9]+>/i', $match)) // Closing tag
				{
					$result .= '<span class="tag">' . stringToHtml($match) . '</span>';
				}
				elseif(preg_match('/<[a-z0-9]+(([\n\s]+)([a-z0-9\-]+)([\n\s]*)(=([\n\s]*)"([^"]*)")?)*([\n\s]*\/)?>/i', $match)) // Opening tag
				{
					$expr2 = '/(<[a-z0-9]+)|([a-z0-9\-]+)|(=)|("([^"]*)")|(\/>)|(.)/i';
					$matches2 = array();
					preg_match_all($expr2, $match, $matches2);

					$result .= '<span class="tag">' . stringToHtml($matches2[0][0]) . '</span>';

					for($j = 1; $j < sizeof($matches2[0]); $j++)
					{
						if(strcasecmp($match2 = $matches2[0][$j], "") !== 0)
						{
							if(strcasecmp("/>", $match2) === 0)
							{
								$result .= '<span class="tag">/&gt;</span>';
							}
							elseif(strcasecmp(">", $match2) === 0)
							{
								$result .= '<span class="tag">&gt;</span>';
							}
							elseif(strcasecmp("=", $match2) === 0)
							{
								$bAsValue = true;
								$result .= '=';
							}
							else
							{
								if(strcasecmp(" ", $match2) === 0)
								{
									$result .= "&nbsp;";
								}
								elseif(strcasecmp("\t", $match2) === 0)
								{
									$result .= "&nbsp;&nbsp;&nbsp;&nbsp;";
								}
								elseif(strcasecmp("\r", $match2) === 0)
								{
									// Ignore
								}
								elseif(strcasecmp("\n", $match2) === 0)
								{
									$result .= "<br>";
								}
								else
								{
									if($bAsValue)
									{
										$result .= '<span class="value">' . stringToHtml($match2) . '</span>';
									}
									else
									{
										$result .= '<span class="attribute">' . stringToHtml($match2) . '</span>';
									}
								}

								$bAsValue = false;
							}
						}
					}
				}
				else // No tag
				{
					$result .= stringToHtml($match);
				}
			}
		}

		return '<code class="html">' . $result . '</code>';
	}

?>
[/code]

Reacties

0
Nog geen reacties.