Jump to content

Recoloring text


KingOfHeart

Recommended Posts

I need an advance coloring script.

I'm working on a forum where a member can post their script(small/pawn).

When posting I need it to convert certain words to a certain color. Those colors need to be in BBCode (you know, text here).

 

Let me give you a better example.

Let's say users can post their custom php script in a textarea.

I then want all the words "return, if, else, while, do..." to be changed to text here

However if they used a // or /* it needs to show up in green instead (small/pawn coding actually works like this)

 

So how would something like this be done?

Link to comment
https://forums.phpfreaks.com/topic/227332-recoloring-text/
Share on other sites

It's like c++.

 

#include <entity>
#include <general>
new music[2][20]={"Z3 Boss BGM.it","dungeon1.it"};
new death = true;
main()
{ 
if(FirstRun())
{
	CallFunction("l_dmenulib",false,"SetPlayerPosition","nnn",4,6,6);
	SetString("lv1b1shut4",0,"lv1b1door6");
	CallFunction("lv1b1block6",false,"SetPushes","n",0);
	CallFunction("lv1b1block7",false,"SetPushes","n",0);
	CallFunction("lv1b1block8",false,"SetPushes","n",0);
	CallFunction("lv1b1block9",false,"SetPushes","n",0);
	if(!isDead("lv1bossworm"))
	{
		PlayMusic(music[0],true);
		if(!isOpen("lv1mbosschest"))
		{
			SetActiveFlag("lv1mbosschest",false);
			ClearCollisionRect("lv1mbosschest",0);
		}
		death = false;
	}
	if(!isOwned("w_bown"))
	{
		SetItem("lv1mbosschest","w_bown");	
		SetString("lv1mbosschest",0,"w_bown");
	}
	else
	{
		SetItem("lv1mbosschest","_itemrupeeb");//blue rupee
		SetString("lv1mbosschest",0,"");	
	}
}

if(isDead("lv1bossworm"))
{
	if(death == false)
	{
		PlayMusic(music[1],true);
		death = true;
	}
	if(!isActive("lv1mbosschest"))
	{
		PlaySound("ChestAppears.wav",240);
		SetActiveFlag("lv1mbosschest",true);	
	}
	CallFunction("lv1b1shut4",false,"Open","null");
}
} 

A php outputter will not work exactly.

Link to comment
https://forums.phpfreaks.com/topic/227332-recoloring-text/#findComment-1172609
Share on other sites

Not the most elegant example, but it may point the way...

(example output: http://www.nstoia.com/sat/red_blue.php)

<?PHP
$red = "<font color=#cc0000><b><i><u><font size='+2'>return</font></u></i></b></font>";
$blue = "<font color=#0000ff><b><i><font size='+1'>same</font></i></b></font>";
$string = "there was a valley where the people couldn't return the same way they had come. For them to return was more difficult than they imagined";
$pattern = '/return/'; 
$string = preg_replace($pattern,$red,$string);
echo $string . "<br/>";
$pattern = '/same/'; 
$string = preg_replace($pattern,$blue,$string);
echo $string . "<br/>";
$string = str_replace("difficult", "<font color=#cc0000><b><i><u><font size='+2'>difficult</font></u></i></b></font>", $string);
echo $string . "<br/>";
?>

Link to comment
https://forums.phpfreaks.com/topic/227332-recoloring-text/#findComment-1172726
Share on other sites

Well I still don't know if preg is the answer or not, but I found an old script a friend made.

If anyone wants a real challenge by fixing a bug or rescripting this listen closely.

 

http://openzelda.thegaminguniverse.org/functions.php

 

For the most part this script works fine when it comes to recoloring. However look at the function "public"

Everything after /* and before */ should all be the color green, but for some reason it's not.

 

Here is the color script.

function OZCoding($input) {
// Arrays that hold match values
$entity = explode(" ", "AngleMove AngleCollide AllocateStrings CreateEntity CreateEntityWithID ChangeDirection ClearCollisionRect Collide CollideAll CollidePoint CheckForHole CallFunction DeleteEntity GetDamage GetRespawnValue GetLiftLevel GetX GetY GetMoveAngle GetDirection GetSpeed GetSpeedMod GetItem GetImage GetType GetString GetState GetInitialX GetInitialY GetEntityCount GetAnglePointX GetAnglePointY GetWeight GetBounceValue GetValue GetHealth GetMaxHealth GetCurrentEntity GetNextMessage GetGotoNextMessage GetNumNextMessages isOpen isTaken isDead isVisible isActive isCuttable isLarge isPickable isPushed isInteracting MessageMap NextEntity Respawn SetOpenFlag SetTakenFlag SetDeadFlag SetVisibleFlag SetActiveFlag SetCuttableFlag SetLargeFlag SetPickableFlag SetPushedFlag SetInteractingFlag SetDamage SetRespawnValue SetLiftLevel SetX SetY SetPosition SetMoveAngle SetDirection SetSpeed SetSpeedMod SetAngleFromDir SetDirFromAngle SetCollisionRect SetItem SetImage SetType SetString SetActiveDist SetState SetWeight SetBounceValue SetValue SetHealth SetMaxHealth StartEntity isOwned SetOwnedFlag GetParent GetParam SetActiveInGroups");
$functions = explode(" ", "AddAnimframe AKey CreateAnim CreateAnimWithID clamp CreateCounter CreateCounterWithID CheckMask CheckForHoleXY ClearRestartTable CalculateAngle DeleteAnim DrawAnim DrawAnimNoInc deleteproperty DeleteCounter DrawImage DownKey DrawText date existproperty EnterKey FinishedAnim funcidx FirstRun FadeTo FinishedReading GetAnimImage GetAnimCount GetMaxAnim GetAnimWidth GetAnimHeight getchar getstring getvalue getarg getproperty GetCounterValue GetWidth GetHeight GetWorldX GetWorldY GetGroupMinX GetGroupMinY GetGroupMaxX GetGroupMaxY GetPauseLevel GetLowerLevelX GetLowerLevelY GetLastAnswer GetMinuteCount GetPlayerName GetHourCount GetDayCount GetGlobal GetLocal heapspace IncrementAnim IncCounterTarget IncCounterValue InitialPlayerX InitialPlayerY InitTextBox LeftKey min max numargs NearPoint print printf PutSprite PlaySound PlayMusic QKey random RightKey SetAnimSpeed SetAnimCount SetAnimDirection SetAnimLoop setarg strlen strcmp strcpy strpack strunpack swapchars setproperty SetCounterMin SetCounterMax SetCounterValue SetCounterTarget SetCounterSpeed SKey SetQKey SetAKey SetSKey SetWKey SetEnterKey SetUpKey SetDownKey 
SetLeftKey SetPlayerName SetRightKey SetWorldX SetWorldY SetPauseLevel SetRestartTable SetRestartPoint SetLowerLevel SetTextBoxColor SetMinuteCount SetHourCount SetDayCount SetDayLength SetGlobal SetLocal SetMusicVolume SetMusicFadeSpeed tolower toupper ToString ToFile TextFile TestFunc time tickcount UpKey UpdateWorldCo WKey Wipe GetTimeDelta floatstr floatround DrawRectangle DrawLine SetTextSize EnableControls DrawBitmapText SetFloat GetFloat SetSpriteAlpha GetCounterMin");

$enum = explode(" ", "black blue cyan green magenta red yellow white chasing dead dying falling falling2 hit jumping knocked lifting standing swimming standLift using walking walkLift scared spining drowning doorType enemyType itemType npcType otherType playerType weaponType switchType warpType east north northeast northwest south southeast southwest west waiting stunned frozen burning sleeping riding wishing pushed pushing floating leaping collecting pulling diveingstill diveingmove");
$keycode = explode(" ", "assert break case char continue default defined do else exit for goto if return sizeof sleep switch while");
$keyword = explode(" ", "false true cellbits cellmax cellmin charbits charmax charmin debug _Small scope_keywords bool const enum fixed float forward native new operator this public static stock");

$out = $input;
$out = str_replace("<","<",$out);
$out = str_replace(">",">",$out);
$out = str_replace("<pre>","<pre>",$out);
$out = str_replace("</pre>","</pre>",$out);

// Fix "'s
$max = strlen($out);
$step = 0;
$temp = $out;
$out = "";
for($i = 0; $i < $max; $i++)
{
	if($step == 0)
	{
		if(ord(substr($temp, $i, 1)) == 226)//226
		{
			$temp2 = substr($temp, $i, 1);
			$step++;
		}
		else
			$out .= substr($temp, $i, 1);
	}
	else if($step == 1)
	{
		$temp2 .= substr($temp, $i, 1);
		if(ord(substr($temp, $i, 1)) == 128)//128
		{
			$step++;
		}
		else
		{
			$out .= $temp2;
			$step = 0;
		}
	}
	else if($step == 2)
	{
		$temp2 .= substr($temp, $i, 1);
		if(ord(substr($temp, $i, 1)) == 156 || ord(substr($temp, $i, 1)) == 157)//156 or 157
			$out .= "\"";
		else
			$out .= $temp2;
		$step = 0;
	}
}

// Fix Tabs
$max = strlen($out);
$temp = $out;
$out = "";
for($i = 0; $i < $max; $i++)
{
	if(ord(substr($temp, $i, 1)) == 9)
		$out .= "    ";
	else
		$out .= substr($temp, $i, 1);
}

//  Handle "
$temp = explode("\"", $out);
$out = "";
$in = 0;
$i = 0;
$com = 0;
while($i < count($temp))
{
	if(count($temp) == 1)
		$out = $temp[$i];
	else {
		$j = $i;
		$tmp3 = "";
		while(explode("\n", $temp[$j]) == 1) {
		$tmp3 = $temp[$j].$tmp3;
		$j--;
		}
		$tmp3 = $temp[$j].$tmp3;
		$temp2 = explode("\n", $tmp3);
		$com = commented($temp2[count($temp2)-1], $com);
		if($in == 0 && !$com && $i < (count($temp)-1)) {
			$in = 1;
			$out .= $temp[$i]."\"<span class=\"ozquote\">";
		}
		else if($in == 1 && $i < (count($temp)-1) && !$com) {
			$in = 0;
			$out.= $temp[$i]."</span>\"";
		}
		else if($com && $i < (count($temp)-1))
			$out .= $temp[$i]."\"";
		else
			$out .= $temp[$i];
	}
	$i++;
}

//  Handle '
$temp = explode("'", $out);
$in = 0;
$i = 0;
$com = 0;
while($i < count($temp))
{
	if($i == 0 && count($temp) > 1)
		$out = $temp[$i]."'";
	else if(count($temp) == 1)
		$out = $temp[$i];
	else {
		$com = commented($temp[$i], $com);
		if($in == 0 && !$com && $i < (count($temp)-1)) {
			$in = 1;
			$out .= "<span class=\"ozquote\">".$temp[$i]."</span>'";
		}
		else if($in == 1 && $i < (count($temp)-1) && !$com) {
			$in = 0;
			$out.= $temp[$i]."'";
		}
		else
			$out .= $temp[$i];
	}
	$i++;
}

// Entity functions
for($i = 0; $i < count($entity); $i++)
{
$temp = explode($entity[$i], $out);
$j = 0;
$q = 0;
$sq = 0;
$com = 0;
while($j < count($temp))
{
	if($j == 0)
		$out = $temp[$j];
	else {
		$temp2 = explode("\n", $temp[($j-1)]);
		$com = commented($temp2[count($temp2)-1], $com);
		$before = ord(substr($temp[($j-1)], -1));
		$after = ord(substr($temp[$j], 0, 1));
		if(notFunc($before, $after) || isodd($q) || isodd($sq) || $com > 0)
			$out .= $entity[$i].$temp[$j];
		else
			$out .= "<span class=\"ozentity\">".$entity[$i]."</span>".$temp[$j];
		}
	$z = $q;
	$q += countquote($temp[$j], $q, $sq);
	$sq += countsquote($temp[$j], $sq, $z);
	$j++;
}
}

// Functions
for($i = 0; $i < count($functions); $i++)
{
$temp = explode($functions[$i], $out);
$j = 0;
$q = 0;
$sq = 0;
$com = 0;
while($j < count($temp))
{
	if($j == 0)
		$out = $temp[$j];
	else {
		$temp2 = explode("\n", $temp[($j-1)]);
		$com = commented($temp2[count($temp2)-1], $com);
		$before = ord(substr($temp[($j-1)], -1));
		$after = ord(substr($temp[$j], 0, 1));
		if(notFunc($before, $after) || isodd($q) || isodd($sq) || $com > 0)
			$out .= $functions[$i].$temp[$j];
		else
			$out .= "<span class=\"ozfunction\">".$functions[$i]."</span>".$temp[$j];
	}
	$z = $q;
	$q += countquote($temp[$j], $q, $sq);
	$sq += countsquote($temp[$j], $sq, $z);
	$j++;
}
}

// Key Codes
for($i = 0; $i < count($keycode); $i++)
{
$temp = explode($keycode[$i], $out);
$j = 0;
$q = 0;
$sq = 0;
$com = 0;
while($j < count($temp))
{
	if($j == 0)
		$out = $temp[$j];
	else {
		$temp2 = explode("\n", $temp[($j-1)]);
		$com = commented($temp2[count($temp2)-1], $com);
		$before = ord(substr($temp[($j-1)], -1));
		$after = ord(substr($temp[$j], 0, 1));
		if(notFunc($before, $after) || isodd($q) || isodd($sq) || $com > 0)
			$out .= $keycode[$i].$temp[$j];
		else
			$out .= "<span class=\"ozkeycode\">".$keycode[$i]."</span>".$temp[$j];
		}
	$z = $q;
	$q += countquote($temp[$j], $q, $sq);
	$sq += countsquote($temp[$j], $sq, $z);
	$j++;
}
}

// Keywords
for($i = 0; $i < count($keyword); $i++)
{
$temp = explode($keyword[$i], $out);
$j = 0;
$q = 0;
$sq = 0;
$com = 0;
while($j < count($temp))
{
	if($j == 0)
		$out = $temp[$j];
	else {
		$temp2 = explode("\n", $temp[($j-1)]);
		$com = commented($temp2[(count($temp2)-1)], $com);
		$before = ord(substr($temp[($j-1)], -1));
		$after = ord(substr($temp[$j], 0, 1));
		if(notFunc($before, $after) || isodd($q) || isodd($sq) || $com > 0)
			$out .= $keyword[$i].$temp[$j];
		else
			$out .= "<span class=\"ozkey\">".$keyword[$i]."</span>".$temp[$j];
		}
	$z = $q;
	$q += countquote($temp[$j], $q, $sq);
	$sq += countsquote($temp[$j], $sq, $z);
	$j++;
}
}

// Enums
for($i = 0; $i < count($enum); $i++)
{
$temp = explode($enum[$i], $out);
$j = 0;
$q = 0;
$sq = 0;
$com = 0;
while($j < count($temp))
{
	if($j == 0)
		$out = $temp[$j];
	else {
		$temp2 = explode("\n", $temp[($j-1)]);
		$com = commented($temp2[count($temp2)-1], $com);
		$before = ord(substr($temp[($j-1)], -1));
		$after = ord(substr($temp[$j], 0, 1));
		if(notFunc($before, $after) || isodd($q) || isodd($sq) || $com > 0)
			$out .= $enum[$i].$temp[$j];
		else
			$out .= "<span class=\"ozenum\">".$enum[$i]."</span>".$temp[$j];
		}
	$z = $q;
	$q += countquote($temp[$j], $q, $sq);
	$sq += countsquote($temp[$j], $sq, $z);
	$j++;
}
}

//  Handle /*
$temp = explode("/*", $out);
$out = $temp[0];
$i = 1;
$q = 0;
$sq = 0;
while($i < count($temp))
{
	if(isodd($q) || isodd($sq))
		$out .= "/*".$temp[$i];
	else
		$out .= "<span class=\"ozcomment\">/*".$temp[$i];
	$i++;
	$z = $q;
	$q += countquote($temp[$j], $q, $sq);
	$sq += countsquote($temp[$j], $sq, $z);
}

//  Handle */
$temp = explode("*/", $out);
$out = "";
$i = 0;
$q = 0;
$sq = 0;
while($i < (count($temp)-1))
{
	if(isodd($q) || isodd($sq))
		$out .= $temp[$i]."*/";
	else
		$out .= $temp[$i]."*/</span>";
	$i++;
	$z = $q;
	$q += countquote($temp[$j], $q, $sq);
	$sq += countsquote($temp[$j], $sq, $z);
}
$out .= $temp[$i];

// Handle #
$temp = explode("\n", $out);
$out = "";
$i = 0;
$c = 0;
$com = 0;
$q = 0;
$sq = 0;
while($i < count($temp))
{
	$temp2 = explode("#", $temp[$i]);
	$com &= 2;
	if(count($temp2) > 1)
	{
		$keyed = 0;
		$j = 1;
		$com = commented($temp2[0], $com);
		$out .= $temp2[0];
		while($j < count($temp2))
		{
			if(!$com && !$keyed && !isodd($q) && !isodd($sq))
			{
				$out .= "<span class=\"ozkey\">#".$temp2[$j];
				$keyed = 1;
			}
			else
				$out .= "#".$temp2[$j];
			$com = commented($temp2[$j], $com);
			$j++;
		}
		$out .= "</span>";
		if($i < (count($temp)-1))
			$out .= "\n";
	}
	else
	{
		$out .= $temp[$i];
		if($i < (count($temp)-1))
			$out .= "\n";
	}
	$i++;
	$z = $q;
	$q += countquote($temp[$j], $q, $sq);
	$sq += countsquote($temp[$j], $sq, $z);
}

// Handle //
$temp = explode("\n", $out);
$out = "";
$i = 0;
$c = 0;
$q = 0;
$sq = 0;
while($i < count($temp))
{
	$temp2 = explode("//", $temp[$i]);
	if(count($temp2) > 1)
	{
		$keyed = 0;
		$j = 1;
		$out .= $temp2[0];
		while($j < count($temp2))
		{
			if(!$com && !$keyed && !isodd($q) && !isodd($sq))
			{
				$out .= "<span class=\"ozcomment\">//".$temp2[$j];
				$keyed = 1;
			}
			else
				$out .= "//".$temp2[$j];
			$j++;
		}
		if($keyed)
			$out .= "</span>";
		if($i < (count($temp)-1))
			$out .= "\n";
	}
	else
	{
		$out .= $temp[$i];
		if($i < (count($temp)-1))
			$out .= "\n";
	}
	$z = $q;
	$q += countquote($temp[$j], $q, $sq);
	$sq += countsquote($temp[$j], $sq, $z);
	$i++;
}

// Handle Tab
$temp = explode("    ", $out);
$out = "";
$i = 0;
while($i < count($temp)-1)
{
	$out .= $temp[$i] . "    ";
	$i++;
}
$out .= $temp[$i];

// Handle \n
$temp = explode("\n", $out);
$out = "";
$i = 0;
while($i < count($temp)-1)
{
	if($i == 0 && $temp[$i] == "")
		$out .= $temp[$i];
	else
		$out .= $temp[$i] . "<br>";
	$i++;
}
$out .= $temp[$i];

return $out;
}

function commented($string, $a)
{
$b = $a;
$tmp = explode("//", $string);
if(count($tmp) > 1)
	$b |= 1;
$tmp = explode("/*", $string);
if(count($tmp) > 1)
	$b |= 2;
$tmp = explode("*/", $string);
if(count($tmp) > 1)
	$b &= 1;
$tmp = explode("\n", $string);
if(count($tmp) > 1)
	$b &= 2;
return $b;
}
function notFunc($a, $b)
{
		$no = 0;
		if($a == 34 || $b == 34)
			$no = 1;
		if($a > 47 && $a < 58)
			$no = 1;
		if($a > 64 && $a < 91)
			$no = 1;
		if($a > 96 && $a < 123)
			$no = 1;
		if($b > 47 && $b < 58)
			$no = 1;
		if($b > 64 && $b < 91)
			$no = 1;
		if($b > 96 && $b < 123)
			$no = 1;
		return $no;

}
function countquote($string, $a, $b)
{
$q = $a;
$sq = $b;
$t = 0;
$tmp = explode("'", $string);
$i = 0;
while($i < count($tmp))
{
	if(!isodd($sq))
	{
		$tmp2 = explode("\"", $tmp[$i]);
		$t += count($tmp2) - 1;
		$q += count($tmp2) - 1;
	}
	if(!isodd($q))
		$sq++;
	$i++;
}
return $t;
}
function countsquote($string, $a, $b)
{
$q = $b;
$sq = $a;
$t = 0;
$tmp = explode("\"", $string);
$i = 0;
while($i < count($tmp))
{
	if(!isodd($q))
	{
		$tmp2 = explode("'", $tmp[$i]);
		$t += count($tmp2) - 1;
		$sq += count($tmp2) - 1;
	}
	if(!isodd($sq))
		$q++;
	$i++;
}
return $t;
}
function isodd($a)
{
$b = $a & 1;
return $b;
}

Since it's the best script I have so far, I might just deal with this bug.

Link to comment
https://forums.phpfreaks.com/topic/227332-recoloring-text/#findComment-1173885
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.