neku
Codeur roumain
Voile je vais partager un petit script php que j'ai fais lorsque je programmais encore en VB qui permet d'éffectuer une colorisation syntaxique de vos codes source sur une page web.
L'appel ce fait comme suit :
le seul argument qui change est donc $source, qui est la source du texte
Code:
// Les mots à coloriser
$ListMot = Array("ReDim ", " And ", " As", " Boolean", "ByRef ", " Byte", "ByVal ", "Call ", "Case ", "Case Is ", "Close ", "Const ", "Declare ", "Dim ", "Do", "DoEvents", "Else", "ElseIf ", "End", "Enum ", "Error ", "Events ", "Exit", "Exit For", "False", "FileCopy", "For ", "Function ", "GoTo ", "If ", "Input", " Integer", "Line", " Long", "Loop", "New ", "Next ", "Not ", "Object", " On", "Open ", "Option Explicit", " Or ", "Output", "Print ", "Private ", "Public ", "Resume ", "Select ", "Set ", " Single", " String", "Sub", " Then", " To ", " True", "Type ", "Wend", "While", "With ");
//$source = str_replace($dim,$dim_replace,$source);
function Majuscules($Source,$ListMot) {
// On met la casse correct sur les mot qui seront colorisé
for ($i = 0;$i <= sizeof($ListMot); $i++) {
$Debut=1;
do {
$SourceUp = strtoupper($Source);
$MotUp = strtoupper($ListMot[$i]);
if (strlen($MotUp) > 0) {
$DebutMot = strpos($SourceUp,$MotUp,$Debut);
if (ord($DebutMot) != 0) {
$Debut = $DebutMot + strlen($ListMot[$i]);
$TextLine = substr($Source, $DebutMot, strlen($ListMot[$i]) );
$Source = str_replace($TextLine,$ListMot[$i],$Source);
}
}
} while (ord($DebutMot) != 0);
}
Convertir($Source,$ListMot);
}
function Convertir($Source,$ListMot) {
// Les couleurs
$bleu="<font color=".chr(34)."#0000FF".chr(34).">";
$noir="<font color=".chr(34)."#000000".chr(34).">";
$vert="<font color=".chr(34)."#009000".chr(34).">";
//On remplace chr(13) et chr(10) par <br>
$Source = str_replace(chr(10),chr(13),$Source);
$Source = str_replace(chr(13).chr(13),chr(13),$Source);
$Source = str_replace(chr(13),"<br>",$Source);
// On colorise
for ($i = 0;$i <= sizeof($ListMot);$i++) {
$Source = str_replace($ListMot[$i],$bleu.$ListMot[$i].$noir,$Source);
}
// On colorise les commentaires
$Debut=1;
do {
$DebutMot = strpos($Source,"'",$Debut);
if ($DebutMot != 0) {
$Debut = strpos($Source,"<br>",$DebutMot);
$TextLine = substr($Source,$DebutMot,$Debut - $DebutMot);
if (substr($Source,$DebutMot - 1,1) == chr(32) || substr($Source,$DebutMot - 1,1) == ">") {
$TextLine2 = str_replace($bleu,"",$TextLine);
$TextLine2 = str_replace($noir,"",$TextLine2);
$TextLine2 = $vert.$TextLine2.$noir;
$Source = str_replace($TextLine,$TextLine2,$Source);
}
$Debut = strpos($Source,"<br>",$DebutMot);
}
} While ($DebutMot != 0);
// On Remplace le double espace chr(32) par 2x Espace Html =>
$Source = str_replace(chr(32).chr(32)," ",$Source);
// On affiche le résultat
echo $Source;
}
Code:
Majuscules($Source,$ListMot);