J'ai créé pour mon site (forums, tutos, mp etc.) une fonction qui permet de colorer syntaxiquement les langages suivants :
- Le PHP
- Le (X)HTML
- Le SQL
- Le CSS
- Le ActionScript
Cette fonction utilise en particuliers les expressions régulières et plus précisément la fonction preg_replace();. Je pense en faire un tuto sur mon site, mais pour ceux que ça intéresse :
<?php
/*
|----------------------------------------------------------------|
| Utilisation / modification autorisées |
| © Alexbad 2006 |
|----------------------------------------------------------------|
*/
function couleur($langage,$source)
{
$langages = array('sql', 'html', 'xhtml', 'php', 'css', 'as');
if(in_array($langage, $langages))
{
if ($langage == 'sql')
{
$source = preg_replace('@INSERT@iU', '<span style="color: #0000ff; font-weight: bold;">INSERT</span>', $source);
$source = preg_replace('@INTO@iU', '<span style="color: #0000ff; font-weight: bold;">INTO</span>', $source);
$source = preg_replace('@SELECT@iU', '<span style="color: #0000ff; font-weight: bold;">SELECT</span>', $source);
$source = preg_replace('@VALUES@iU', '<span style="color: #0000ff; font-weight: bold;">VALUES</span>', $source);
$source = preg_replace('@GRANT@iU', '<span style="color: #0000ff; font-weight: bold;">GRANT</span>', $source);
$source = preg_replace('@USAGE@iU', '<span style="color: #0000ff; font-weight: bold;">USAGE</span>', $source);
$source = preg_replace('@FROM@iU', '<span style="color: #0000ff; font-weight: bold;">FROM</span>', $source);
$source = preg_replace('@ORDER BY@iU', '<span style="color: #0000ff; font-weight: bold;">ORDER BY</span>', $source);
$source = preg_replace('@LIMIT@iU', '<span style="color: #0000ff; font-weight: bold;">LIMIT</span>', $source);
$source = preg_replace('@ASC@iU', '<span style="color: #0000ff; font-weight: bold;">ASC</span>', $source);
$source = preg_replace('@DESC@U', '<span style="color: #0000ff; font-weight: bold;">DESC</span>', $source);
$source = preg_replace('@CREATE@iU', '<span style="color: #0000ff; font-weight: bold;">CREATE</span>', $source);
$source = preg_replace('@DATABASE@iU', '<span style="color: #0000ff; font-weight: bold;">DATABASE</span>', $source);
$source = preg_replace('@TABLE@iU', '<span style="color: #0000ff; font-weight: bold;">TABLE</span>', $source);
$source = preg_replace('@ALTER@iU', '<span style="color: #0000ff; font-weight: bold;">ALTER</span>', $source);
$source = preg_replace('@DROP@iU', '<span style="color: #0000ff; font-weight: bold;">DROP</span>', $source);
$source = preg_replace('@TRUNCATE@iU', '<span style="color: #0000ff; font-weight: bold;">TRUNCATE</span>', $source);
$source = preg_replace('@AS@U', '<span style="color: #0000ff; font-weight: bold;">AS</span>', $source);
$source = preg_replace('@NULL@iU', '<span style="color: #0000ff; font-weight: bold;">NULL</span>', $source);
$source = preg_replace('@NOT@iU', '<span style="color: #0000ff; font-weight: bold;">NOT</span>', $source);
$source = preg_replace('@AUTO_INCREMENT@iU', '<span style="color: #0000ff; font-weight: bold;">AUTO_INCREMENT</span>', $source);
$source = preg_replace('@PRIMARY@iU', '<span style="color: #0000ff; font-weight: bold;">PRIMARY</span>', $source);
$source = preg_replace('@KEY@iU', '<span style="color: #0000ff; font-weight: bold;">KEY</span>', $source);
//Types de champs
$source = preg_replace('@VARCHAR@iU', '<span style="color: #0000ff; font-weight: bold;">VARCHAR</span>', $source);
$source = preg_replace('@MEDIUMINT@iU', '<span style="color: #0000ff; font-weight: bold;">MEDIUMINT</span>', $source);
}
if ($langage == 'php')
{
$source = htmlentities($source);
$source = preg_replace('@<\?(php3|php4|php5|php|)(.*)\?>@iUs', '<span style="color: #ff0000;"><?$1</span>$2<span style="color: #ff0000;">?></span>', $source);
$source = preg_replace('@\\\'(.*)\\\'@i', '<span style="color: #808080;">\'$1\'</span>', $source);
$source = preg_replace('@(switch|case|break|array|return|function|echo|print|if|elseif|else|isset|var|global|define|include|require|include_once|require_once|for|while|do)( |\n|;|\))@i', '<span style="color: #0ed600; text-decoration:underline;">$1</span>$2', $source);
$source = preg_replace('@\$([a-zA-Z0-9_]{1,})(\+|-|;| |\))@iU', '<span style="color: #0000ff;">$$1</span>$2', $source);
$source = preg_replace('@\$([a-zA-Z0-9_]{1,})\+@iU', '<span style="color: #0000ff;">$$1</span>+', $source);
$source = preg_replace('@\$([a-zA-Z0-9_]{1,})-@iU', '<span style="color: #0000ff;">$$1</span>-', $source);
$source = preg_replace('@\$([a-zA-Z0-9_]{1,});@iU', '<span style="color: #0000ff;">$$1</span>;', $source);
$source = preg_replace('@\$([a-zA-Z0-9_]{1,})\)@iU', '<span style="color: #0000ff;">$$1)</span>;', $source);
$source = preg_replace("@//(.*)\\n@iU", "<span style=\"color: green; font-style:italic;\">//$1</span>\n", $source);
}
if ($langage == 'html' || $langage == 'xhtml')
{
$source = htmlentities($source);
$balises = array('link', 'ul', 'li', 'textarea', 'input', 'form', 'label', 'br', 'font', 'frame', 'frameset', 'iframe', 'html', 'body', 'head', 'img', 'script', 'a', 'code', 'pre', 'link', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'center', 'div', 'span', 'meta', 'style', 'b', 'u', 'i', 'em', 'title');
$nbre_balises = count($balises);
for ($i = 0; $i < $nbre_balises; $i++)
{
$source = preg_replace('@<' . $balises[$i] . '(.*)>@isU', '<span style="color: blue;"><' . $balises[$i] . '</span>$1<span style="color: blue;">></span>', $source);
$source = preg_replace('@</' . $balises[$i] . '(.*)>@isU', '<span style="color: blue;"></' . $balises[$i] . '</span>$1<span style="color: blue;">></span>', $source);
}
$attributs = array('href', 'type', 'rel', 'media', 'for', 'action', 'method', 'value', 'bgcolor', 'vlink', 'alink', 'link', 'color', 'size', 'face', 'title', 'href', 'src', 'type', 'style', 'alt', 'id', 'class', 'name', 'content', 'http-equiv', 'language', 'onmouseover', 'onmousedown', 'onkeyup', 'onselect', 'onkeydown', 'onclick', 'rel', 'media');
$nbre_attributs = count($attributs);
for ($i = 0; $i < $nbre_attributs; $i++)
{
$source = preg_replace('@<(.+) ' . $attributs[$i] . '="(.*)"(.*)>@isU', '<$1 <span style="color: red;">' . $attributs[$i] . '="</span><span style="font-weight: bold;">$2</span><span style="color: red;">"</span>$3>', $source);
}
$source = preg_replace('@<!--(.*)-->@isU', '<span style="color: green; font-size: 11px;"><!--$1--></span>', $source);
}
if ($langage == 'css')
{
$source = htmlentities($source);
$source = preg_replace('@([\n\s]{1,})#(.+)([\s\n]{1,})@iUs', '$1<span style="color: #01aff0; font-weight: bold;">#$2</span>$3', $source);
$source = preg_replace('@([\n\s]{1,})\.(.+)([\s\n]{1,})@iUs', '$1<span style="color: red;">.$2</span>$3', $source);
$proprietes = array('border', 'border-top', 'border-bottom', 'border-left', 'border-right', 'border-style', 'border-color', 'border-width', 'background', 'background-color', 'background-image', 'background-repeat', 'color', 'font-size', 'font-weight', 'font-family', 'list-style-type', 'width', 'height', 'position', 'top', 'left', 'right', 'bottom', 'display', 'cursor', 'padding', 'padding-left', 'padding-top', 'padding-right', 'padding-bottom', 'margin', 'margin-left', 'margin-right', 'margin-top', 'margin-bottom', 'text-align', 'text-decoration', 'float', 'font-style', 'font-variant', 'text-transform', 'font', 'vertical-align', 'text-indent', 'line-height', 'vertical-align', 'background-attachment', 'background-position', 'min-width', 'min-height', 'max-width', 'max-height', 'visibility', 'clip', 'clear', 'z-index', 'list-style-position', 'list-style', 'list-style-image', 'border-collapse', 'empty-cells', 'caption-side', 'font-stretch', 'table-layout', 'border-spacing', 'speak-header', 'outline-width', 'outline-style', 'outline-color', 'white-space', 'text-shadow', 'font-size-adjust', 'size', 'marks', 'page', 'orphans', 'page-break-inside', 'page-break-after', 'page-break-before', 'border-top-style', 'border-top-color', 'border-top-width', 'border-left-style', 'border-left-color', 'border-left-width', 'border-bottom-style', 'border-bottom-color', 'border-bottom-width', 'border-right-style', 'border-right-color', 'border-right-width', 'direction', 'unicode-bidi', 'overflow', 'content', 'quotes', 'counter-reset', 'counter-increment', 'marker-offset', 'volume', 'speak', 'pause', 'pause-after', 'pause-before', 'cue', 'cue-before', 'cue-after', 'play-during', 'azimuth', 'elevation', 'speech-rate', 'voice-family', 'pitch', 'pitch-range', 'stress', 'richness', 'speak-punctuation', 'speak-numeral');
$nbre_proprietes = count($proprietes);
for ($i = 0; $i < $nbre_proprietes; $i++)
{
$source = preg_replace("@\n(| ){0,10}" . $proprietes[$i] . ":(.*);@iU", "\n<span style=\"color: green;\">" . $proprietes[$i] . "</span>:<span style=\"font-weight:bold;\">$2;</span>", $source);
}
}
if ($langage == 'as')
{
$words = array('on', 'stopDrag', 'gotoAndPlay', 'gotoAndStop', 'nextFrame', 'nextScene', 'play', 'prevFrame', 'prevScene', 'stop', 'stopAllSounds', 'fscommand', 'getURL', 'loadMovie', 'loadMovieNum', 'loadVariables', 'loadVariablesNum', 'unloadMovie', 'unloadMovieNum', 'print', 'printAsBitmap', 'printAsBitmapNum', 'printNull', 'clearInterval', 'escape', 'eval', 'getTimer', 'getVersion', 'MMExecute', 'setInterval', 'trace', 'unescape', 'isFinite', 'isNaN', 'parseFloat', 'parseInt', 'Array', 'Boolean', 'Number', 'Object', 'String', 'duplicateMovieClip', 'getProperty', 'onClipEvent', 'removeMovieClip', 'setProperty', 'startDrag', 'stopDrag', 'targetPath', 'updateAfterEvent', '_accProps', '_focusRect', '_global', '_level', '_parent', '_root', 'this', '_quality', '_soundbuftime', 'break', 'case', 'continue', 'default', 'do', 'else', 'while', 'if', 'for', 'in', 'switch', 'class', 'dynamic', 'extends', 'get', 'implements', 'import', 'interface', 'intrinsic', 'private', 'public', 'set', 'static', 'super', 'catch', 'finally', 'throw', 'try', 'function', 'return', 'delete', 'var', 'with', 'new LoadVars', 'addRequestHeader', 'decode', 'getBytesLoaded', 'getBytesTotal', 'load', 'send', 'sendAndLoad', 'toString', 'contentType', 'loaded', 'onData', 'onHTTPStatus', 'onLoad', 'XML', 'appendChild', 'cloneNode', 'createElement', 'createTextNode', 'asChildNode', 'insertBefore', 'parseXML', 'removeNode', 'attributesContentType', 'DocTypeDecl', 'firstChild', 'idMap', 'ignoreWhite', 'lastChild', 'nextSibling', 'nodeName', 'nodeType', 'nodeValue', 'parentNode', 'previewSibling', 'status', 'XMLDecl', 'XMLNode');
$nbre_words = count($words);
for ($i = 0; $i < $nbre_words; $i++)
{
$source = preg_replace("@" . $words[$i] . "@", "<span style=\"color:blue;\">" . $words[$i] . "</span>", $source);
}
$source = preg_replace("@//(.*)\\n@iU", "<span style=\"color: green; font-size:11px;\">//$1</span>\n", $source);
$source = preg_replace('@/\*(.*)\*/@isU', '<span style="color: green; font-size: 11px;"><!--$1--></span>', $source);
}
}
else
{
$source = htmlentities($source);
$langage = 'Code';
}
$code = '<p class="abloc">' . strtoupper($langage) . ' :</p><div class="code-tuto"><pre class="langage-' . $langage . '"><code>' . $source . '</code></pre></div>';
return $code;
}
?>
Voilà, si ça peut en aider quelques-uns

[/list]