wc3garage82
New Member
- Messages
- 43
- Reaction score
- 0
- Points
- 0
I'm currently working on tweaking this script for my forum but unfortunately, it took a long time to handle long texts.
This PHP script will parse each line's words and apply appropriate html codes to that word.
Can someone help me optimize it? And make it faster so it can parse long texts faster. Well the original code was calling arrays for each word making it too long but this one will just call an array once. I just want really to make this php script parse a very long text fast (so shorter texts are faster). If anyone may share their scripts like in this one (checking each line and the words in it, and add the appropriate html code to that word)
here:
This PHP script will parse each line's words and apply appropriate html codes to that word.
Can someone help me optimize it? And make it faster so it can parse long texts faster. Well the original code was calling arrays for each word making it too long but this one will just call an array once. I just want really to make this php script parse a very long text fast (so shorter texts are faster). If anyone may share their scripts like in this one (checking each line and the words in it, and add the appropriate html code to that word)
here:
PHP:
/**
* Parse Jass Code MyCode.
*/
function mycode_add_FixWord($word,$native,$blocks,$values,$types,$bjfunc) {
if(in_array($word,$native)){
return "<span style=\"color: purple;\">".$word."</span>";
}
if(in_array($word,$blocks)){
return "<span style=\"font-weight: bold;\">".$word."</span>";
}
if(in_array($word,$values)){
return "<span style=\"color: #3333ff;\">".$word."</span>";
}
if(in_array($word,$types)){
return "<span style=\"color: #4477aa; font-weight: bold;\">".$word."</span>";
}
if(in_array($word,$bjfunc)){
return "<span style=\"color: #dd4444;\">".$word."</span>";
}
return $word;
}
function mycode_parse_jass($text) {
include('plugins/parser/jass.php');
$native = explode(" ", $Natives);
$blocks = explode(" ", $Blocks);
$values = explode(" ", $Values);
$types = explode(" ", $Types);
$bjfunc = explode(" ", $BJ_Func);
//$text = $message[1];
$bb=true;
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
$special = "[]()";
$str = "\"";
$objid = "'";
$trans = Array(">" => ">", "<" => "<", "&" => "&", ":" => ":", " " => " ");
if (!$bb)
{
$text = strtr($text, $trans);
}
else
{
// Do some backwards working
$tr = array('(' => '(',
')' => ')',
'[' => '[',
']' => ']',
'"' => '"');
$text = strtr($text,$tr);
}
$result = "";
$comment = 0;
$Lines = explode("\n", $text);
$anchors = array();
$n = 0;
$line_counter = 0;
// First, find all functions and place an anchor there
foreach($Lines as $Line)
{
// Leave anchor here?
$reg = array();
$anchors[$n] = '';
if (ereg('^ *(constant )? *(function|native) *([a-zA-Z_][a-zA-Z_0-9]*) *takes', $Line, $reg))
{
$l = max(0, $n-12);
$anchors[$l] .= '<a name="'.strtolower($reg[3]).'"></a>';
}
$n++;
}
$n = 0;
foreach($Lines as $Line)
{
$result .= $anchors[$n];
$Line = rtrim($Line);
if ($isString) {
$result = $result . "</span>";
}
$mac = 0;
$mac = strpos($Line, "//!");
if ($mac === false) {
$mac = strlen($Line);
$macros = 0;
} else {
$macros = 1;
}
$stop = strpos($Line, "//");
if ($stop === false) {
$stop = strlen($Line);
$comment = 0;
} else{
$comment = 1;
}
$i = 0;
$word = "";
$isString = 0;
$stringStarter = "";
$slashed = 0;
// Highlight
while ($i < $stop) {
$c = substr($Line, $i, 1);
$i += 1;
if ($isString) {
$result = $result . $c;
if ($c == "\\" || $c == "//") {
$slashed = ($slashed)? 0:1;
} elseif (($c == $stringStarter) && !($slashed)) {
$result = $result . "</span>";
$isString = 0;
} else {
$slashed = 0;
}
} else {
// If normal character
if (strpos($chars, $c) !== false) {
$word = $word . $c;
// If bracket
} elseif (strpos($special, $c) !== false) {
$result = $result . $this->mycode_add_FixWord($word,$native,$blocks,$values,$types,$bjfunc) . $this->mycode_add_FixWord($c,$native,$blocks,$values,$types,$bjfunc);
$word = "";
// If string
} elseif (strpos($str, $c) !== false) {
$result = $result . $this->mycode_add_FixWord($word,$native,$blocks,$values,$types,$bjfunc) . "<span style=\"color: blue\">" . $c;
$slashed = 0;
$word = "";
$isString = 1;
$stringStarter = $c;
} elseif (strpos($objid, $c) !== false) {
$result = $result . $this->mycode_add_FixWord($word,$native,$blocks,$values,$types,$bjfunc) . "<span style=\"color: black;text-decoration:underline\">" . $c;
$slashed = 0;
$word = "";
$isString = 1;
$stringStarter = $c;
// If other character
} else {
$result = $result . $this->mycode_add_FixWord($word,$native,$blocks,$values,$types,$bjfunc) . $c;
$word = "";
}
} // if ($isString)
}
$result = $result . $this->mycode_add_FixWord($word,$native,$blocks,$values,$types,$bjfunc);
// Add Macros
if ($macros) {
$result = $result . "<span style=\"color: #666;\">".strstr($Line, "//!")."</span>";
$comment = 0;
}
// Add comment
if ($comment) {
$result = $result . "<span style=\"color: green;\">".strstr($Line, "//")."</span>";
}
// Add linebreak
$result = $result . "<br />";
$n++;
$line_counter = $line_counter + 16;
}
$result = substr($result, 0, strlen($result)-6); // Remove last linebreak
//fixes for mybb parser
$tr = array('(' => '(',
')' => ')',
'[' => '[',
']' => ']');
$result = strtr($result, $tr);
$line_counter = $line_counter + 16;
if($line_counter > 600){
$line_counter = 600;
}
/*$result = <<<EOT
<div style="margin:20px; font-weight: normal; font-decoration: none; font-style: normal; width:640px;">
<div class="smallfont" style="margin-bottom:2px; text-align:left;">Jass: </div>
<pre style="text-align:left; margin:0px;padding: 6px; color:#000; width:640px; height:{$line_counter}px; overflow:auto; background-color:#fff; border:1px solid black;">
$result
</pre>
</div>
EOT;*/
return "<div style=\"margin:20px; font-weight: normal; font-decoration: none; font-style: normal; width:640px;\"><div class=\"smallfont\" style=\"margin-bottom:2px; text-align:left;\">Jass: </div><pre style=\"text-align:left; margin:0px;padding: 6px; color:#000; width:640px; height:{$line_counter}px; overflow:auto; background-color:#fff; border:1px solid black;\"> {$result} </pre></div>";
}