MrApples
02-03-2008, 07:36 PM
I made a parser which is using too much memory ( 38 megs ), how could I cut that down?
I must be doing something which is bad practice in PHP but I'm new to it.
function fetch_bbcode_trigger_blocks(){
return array(
'If (All Conditions Are True)',
'If - Conditions',
....
Continues for about 10
);
}
function fetch_bbcode_trigger_actions(){
return array(
'Set',
'---',
'Skip Remaining Actions',
....
Continues for about 30
);
}
function fetch_bbcode_trigger_icons(){
return array(
'If (All Conditions Are True)' => 'if',
'If - Conditions' => 'if',
....
Continues for about 40
);
}
function NextLine($pos, $code){
$nextl = ( strpos($code,'<br />',$pos) + 6 );
if ( $nextl > 0 ){
return $next1;
} else {
return -1;
}
}
function IsBlockEnd($code, $len){
$reglen = strlen($code);
$newlen = ($reglen - strlen(ltrim($code,"\t ")));
return ($newlen < $len);
}
function IsBlock($code){
$type = 0;
$blocks = fetch_bbcode_trigger_blocks();
foreach ($blocks as $block){
$spos = stripos($code,$block);
if ( $spos ){
return $block;
}
}
}
function IsAction($code){
$type = 0;
$actions = fetch_bbcode_trigger_actions();
foreach ($actions as $action){
$spos = stripos($code,$action);
if ( $spos ){
return $action;
}
}
}
function IsUnknown($code){
if ( IsBlock($code) ){
return false;
} elseif ( IsAction($code) ){
return false;
} elseif ( strpos($code, '(') ){
return false;
}
return true;
}
function handle_bbcode_trigger($code){
//v global $vbulletin, $vbphrase, $stylevar, $show, $html_allowed;
$trig = '';
// Define Images and HTML
$icons = fetch_bbcode_trigger_icons();
$iempty = '<img height=\"16\" width=\"16\" src=\"/imgs/jass/empty.gif\" alt=\"\"/>';
$iline = '<img height=\"16\" width=\"16\" src=\"/imgs/jass/line.gif\" alt=\"\"/>';
$ijoin = 'join';
$iblock = '<div><a href=\'#top\' onclick=\'return ExpandCollapseNode(this,\"/imgs/jass/joinbottomplus.gif\",\"/imgs/jass/joinbottomminus.gif\")\'><img src=\'/imgs/jass/joinbottomminus.gif\' alt=\'Collapse\' border=\'0\' /></a><img src=\"/imgs/jass/join.gif\" alt=\"\" />';
$ibend = '<img src=\"/imgs/jass/joinbottom.gif\" alt=\"\"/>';
// Start Predefines
$cur = 0;
$setblock = 0;
$blocklevel = 0;
$lastlen = 0;
$nextline = NextLine(0,$code);
$action = '';
$start = '';
$end = '';
$icon = '';
while ( $nextline != -1 || $cur < 2 ){
$cur++;
$line = substr($code, $pos, $nextline);
$pos = $nextline;
if ( $setblock != 0 ){
$blocklevel = ($blocklevel + $setblock);
$setblock = 0;
}
if ( $blocklevel == 0 ){
// Is this a trigger name?
if (!$actiondefined){
if ( IsUnknown($line) ){
// Yis!
$setblock = 1;
$icon = 'base';
}
}
} else {
if ( IsBlockEnd($line,$lastlen) ){
$setblock = -1;
} elseif ( IsBlock($line) ){
$setblock = 1;
$icon = IsBlock($line);
}
}
$lastlen = strlen($line);
// We can clean it now. IsBlockEnd requires whitespaces and tabs.
$line = ltrim($line,"\t ");
if ( $setblock <= 0 ){
// Its a function...
$action = IsAction($line);
if ( $action != '' ){
$icon = $action;
if ( $blocklevel == 0 ){
$actiondefined = 1;
}
} else {
$icon = 'unknown';
}
}
$level = $blocklevel;
if ( $level > 0 ){
while ( $level > 1 ){
if ( $blockhere[$level] == 0 ){
$start .= $iempty;
} else {
$start .= $iline;
}
$level--;
}
if ( $setblock > 0 ){
$start .= $iblock;
} elseif ( $setblock < 0 ){
$start .= $ibend;
} else {
$start .= $ijoin;
}
} elseif ( $setblock > 0 ){
$start .= $iblock;
$start .= $ijoin;
}
if ( $setblock < 0 ){
$end = '</span></div>';
} else {
$end = '</span>';
}
$start .= "<img height=\"16\" width=\"16\" src=\"/imgs/jass/" . $icons[$icon] . ".gif\" alt=\"\" />',";
$start .= "<span>";
$line = $start . $line;
$line .= $end;
$trig .= $line;
$nextline = NextLine($pos, $code);
$action = '';
$start = '';
$end = '';
$icon = '';
}
$code = '';
$replace = array (
'<br>',
'<br />',
);
$with = array (
'',
'',
);
$trig = str_replace($replace, $with, $trig);
return $trig;
}
If it applies the error happens at "$trig .= $line;"
I must be doing something which is bad practice in PHP but I'm new to it.
function fetch_bbcode_trigger_blocks(){
return array(
'If (All Conditions Are True)',
'If - Conditions',
....
Continues for about 10
);
}
function fetch_bbcode_trigger_actions(){
return array(
'Set',
'---',
'Skip Remaining Actions',
....
Continues for about 30
);
}
function fetch_bbcode_trigger_icons(){
return array(
'If (All Conditions Are True)' => 'if',
'If - Conditions' => 'if',
....
Continues for about 40
);
}
function NextLine($pos, $code){
$nextl = ( strpos($code,'<br />',$pos) + 6 );
if ( $nextl > 0 ){
return $next1;
} else {
return -1;
}
}
function IsBlockEnd($code, $len){
$reglen = strlen($code);
$newlen = ($reglen - strlen(ltrim($code,"\t ")));
return ($newlen < $len);
}
function IsBlock($code){
$type = 0;
$blocks = fetch_bbcode_trigger_blocks();
foreach ($blocks as $block){
$spos = stripos($code,$block);
if ( $spos ){
return $block;
}
}
}
function IsAction($code){
$type = 0;
$actions = fetch_bbcode_trigger_actions();
foreach ($actions as $action){
$spos = stripos($code,$action);
if ( $spos ){
return $action;
}
}
}
function IsUnknown($code){
if ( IsBlock($code) ){
return false;
} elseif ( IsAction($code) ){
return false;
} elseif ( strpos($code, '(') ){
return false;
}
return true;
}
function handle_bbcode_trigger($code){
//v global $vbulletin, $vbphrase, $stylevar, $show, $html_allowed;
$trig = '';
// Define Images and HTML
$icons = fetch_bbcode_trigger_icons();
$iempty = '<img height=\"16\" width=\"16\" src=\"/imgs/jass/empty.gif\" alt=\"\"/>';
$iline = '<img height=\"16\" width=\"16\" src=\"/imgs/jass/line.gif\" alt=\"\"/>';
$ijoin = 'join';
$iblock = '<div><a href=\'#top\' onclick=\'return ExpandCollapseNode(this,\"/imgs/jass/joinbottomplus.gif\",\"/imgs/jass/joinbottomminus.gif\")\'><img src=\'/imgs/jass/joinbottomminus.gif\' alt=\'Collapse\' border=\'0\' /></a><img src=\"/imgs/jass/join.gif\" alt=\"\" />';
$ibend = '<img src=\"/imgs/jass/joinbottom.gif\" alt=\"\"/>';
// Start Predefines
$cur = 0;
$setblock = 0;
$blocklevel = 0;
$lastlen = 0;
$nextline = NextLine(0,$code);
$action = '';
$start = '';
$end = '';
$icon = '';
while ( $nextline != -1 || $cur < 2 ){
$cur++;
$line = substr($code, $pos, $nextline);
$pos = $nextline;
if ( $setblock != 0 ){
$blocklevel = ($blocklevel + $setblock);
$setblock = 0;
}
if ( $blocklevel == 0 ){
// Is this a trigger name?
if (!$actiondefined){
if ( IsUnknown($line) ){
// Yis!
$setblock = 1;
$icon = 'base';
}
}
} else {
if ( IsBlockEnd($line,$lastlen) ){
$setblock = -1;
} elseif ( IsBlock($line) ){
$setblock = 1;
$icon = IsBlock($line);
}
}
$lastlen = strlen($line);
// We can clean it now. IsBlockEnd requires whitespaces and tabs.
$line = ltrim($line,"\t ");
if ( $setblock <= 0 ){
// Its a function...
$action = IsAction($line);
if ( $action != '' ){
$icon = $action;
if ( $blocklevel == 0 ){
$actiondefined = 1;
}
} else {
$icon = 'unknown';
}
}
$level = $blocklevel;
if ( $level > 0 ){
while ( $level > 1 ){
if ( $blockhere[$level] == 0 ){
$start .= $iempty;
} else {
$start .= $iline;
}
$level--;
}
if ( $setblock > 0 ){
$start .= $iblock;
} elseif ( $setblock < 0 ){
$start .= $ibend;
} else {
$start .= $ijoin;
}
} elseif ( $setblock > 0 ){
$start .= $iblock;
$start .= $ijoin;
}
if ( $setblock < 0 ){
$end = '</span></div>';
} else {
$end = '</span>';
}
$start .= "<img height=\"16\" width=\"16\" src=\"/imgs/jass/" . $icons[$icon] . ".gif\" alt=\"\" />',";
$start .= "<span>";
$line = $start . $line;
$line .= $end;
$trig .= $line;
$nextline = NextLine($pos, $code);
$action = '';
$start = '';
$end = '';
$icon = '';
}
$code = '';
$replace = array (
'<br>',
'<br />',
);
$with = array (
'',
'',
);
$trig = str_replace($replace, $with, $trig);
return $trig;
}
If it applies the error happens at "$trig .= $line;"