السلام عليكم ورحمة الله وبركاته
- تلك الدالة تخرج كود لغة c منسق فى صفحة HTML .
- وما دفعنى لكتابة دالة لهذا الغرض هو أننى وجدت صعوبة فى تكرار تنسيق الكود داخل ملف MS Word
- ومن خلال تلك الدالة تستطيع حفظ الصفحة كصفحة HTML وتقو بإدراجها داخل ملف Word عن طريق Insert >>File
وسوف يظهر الكود بنفس تنسيقة داخل وثيقة ال Word
- ما يعيب الدالة هو تلوين الكلمات المحجوزة من اللغة إذا ظهرت فى التعليق أو النص الثابت " سأحاول تعديل الدالة لاحقاً "
إليكم كود الدالة :
<?php
function cLanguageFormat($Text){
// إزالة الوسوم لتظهر عند عرضها على المتصفح
$Text = str_replace("<","<",$Text);
$Text = str_replace(">",">",$Text);
// هذا للتأكد من أن النص تعليق
$isComment= false;
// للتأكد من أن النص ثابت بين علامتى إقتباس " "
$isConstantText= False;
// مصفوفة بالكلمات المحجوزة فى اللغة التى تسوف تأخذ لون معين
$ReservedwordsBlue = array("\nvoid "," void ","\nreturn "," nreturn ","\nint "," int ","\nlong "," long ","\ndouble "," double ","\nfloat "," float ",
"\nshort "," short ","\nchar "," char ","\nsigned "," signed ","\nunsigned "," unsigned ","\nbreak "," break ",
"\ncontinue "," continue ","\ndefault "," default ","\switch "," switch ","\ncase "," case ","\nwhile "," while ",
"\nif"," if ","\nelse "," else ","\ndo "," do ","\nsizeof "," sizeof ","\ngoto "," goto ","\nstatic"," static ",
"\nstruct "," struct ","\ntypedef "," typedef ","\nunion "," union ","\nregister "," register ","\nenum "," enum ",
"\nconst "," const ","\nvolatile "," volatile ","\nauto "," auto ");
// مصفوفة أخرى لكلمات تضمين الملفات لتكون بلون معين
$ReservedwordsRed = array("#include "," #include ","#define "," #define ");
// مصفوفة لحفظ كل تعليق على حده
$CommentText="";
// مصفوفة لحفظ النص الثابت بين علامتى إقتباس
$ConstantText= "";
// عداد مؤقت لحفظ التعليقات تمهيدأ لإسنادها لمصفوفة التعليقات
$CommentTextCount="";
// عداد مؤقت لحفظ النصوص الثابتة تمهيداً لإسنادها لمصفوفة النصوص الثابتة
$ConstantTextCount= "";
// عداد التعليقات
$CommentCount=-1;
// عداد النصوص الثابتة
$ConstantCount= -1;
for($count=0;$count<strlen($Text);$count++)
{
if($Text[$count]== "/" && $Text[$count+1]== "*") // تحقق هذا الشرط يعنى بداية التعليق
{
$isComment= True;
$CommentCount++;
}
if($isComment != True && $Text[$count] == '"' && $isConstantText == False) // تحقق هذا الشرط يعنى بداية النص الثابت
{
$isConstantText= True;
$ConstantCount++;
$ConstantTextCount.= $Text[$count];
goto a; // لكى يتخطى إختبار الشرطين التاليين فى تلك الدوره
}
if($isConstantText == True) // إذا تحقق الشرط يقوم بتخزين الحروف فى المتغير المؤقت لحفظ النص الثابت
{
$ConstantTextCount.= $Text[$count];
}
if($isComment != True && $Text[$count] == '"' && $isConstantText == True) // شرط تحقق نهاية النص الثابت
{
$isConstantText= False;
$ConstantText[$ConstantCount]=$ConstantTextCount;
$ConstantTextCount="";
}
a:
if($isComment == True) // إذا كان هذا الشرط متحقق يتم إضافة الأحرف لعداد التعليقات المؤقت
{
$CommentTextCount.=$Text[$count];
}
if($Text[$count]== "*" && $Text[$count+1]== "/") // تحقق شرط نهاية التعليق
{
$isComment= False;
$CommentText[$CommentCount]=$CommentTextCount."/";
$CommentTextCount= "";
}
}
for($i=0;$i<=$CommentCount;$i++) // إضافة وسوم ومعرف للتعليقات لتغيير لونها
{
$Text = str_replace($CommentText[$i],"<span class='Comment'>".$CommentText[$i]."</span>",$Text);
}
for($i=0;$i<=$ConstantCount;$i++) // إضافة وسوم ومعرف للنصوص الثابتة لتغيير لونها
{
$Text = str_replace($ConstantText[$i],"<span class='Constant'>".$ConstantText[$i]."</span>",$Text);
}
for($i=0;$i<count($ReservedwordsBlue);$i++) // إضافة وسوم ومعرف للكلمات المحجوزة لتغيير لونها
{
$Text = str_replace($ReservedwordsBlue[$i]," <span class='ReservedwordsBlue'> ".$ReservedwordsBlue[$i]." </span> ",$Text);
$Text = str_replace("\n","</br>",$Text); // نزول سطر جديد فى المتصفح
}
for($i=0;$i<count($ReservedwordsRed);$i++) // إضافة وسوم ومعرف للكلمات محجوزة أخرى فى اللغة لتغيير لونها
{
$Text = str_replace($ReservedwordsRed[$i],"<span class='ReservedwordsRed'>".$ReservedwordsRed[$i]."</span>",$Text);
}
return "<div class='BG'>".$Text."</div>"; // عودة بكامل الكود داخل وسم ومعرف للتحكم فى خلفيتة وإطاره وخصائص أخرى
}
?>وهذا كود التنسيق تستطيع من خلالة التحكم فى ألوان الكلمات والخلفية "يوضع فى ملف css أو فى سكريبت فى رأس الصفحة ":
.
BG{
background-color:#d0e4fe;
}
.ReservedwordsBlue{
color: #0000FF;
}
.ReservedwordsRed{
color: #FF0000;
}
.Comment {
color: #33AA33;
}
.Constant {
color: #999999;
}وإليكم مثال على الدالة فى المرفقات