Log in

View Full Version : Code optimization


kmike
04-28-2005, 12:34 PM
It is possible to reduce code size by some twenty lines, using regular expressions.
in includes/functions_showthread.php, whole fragment

$admgroups=explode("|",$vboptions['warn_admingroups']);
$modgroups=explode("|",$vboptions['warn_modgroups']);
$smodgroups=explode("|",$vboptions['warn_smodgroups']);

if($admgroups[0])
{
foreach($admgroups as $adm)
{
if ($adm==$bbuserinfo['usergroupid'])
{
$useradmin=1;
}
}
}
if($modgroups[0])
{
foreach($modgroups as $mod)
{
if ($mod==$bbuserinfo['usergroupid'])
{
$usermod=1;
}
}
}
if($smodgroups[0])
{
foreach($smodgroups as $smod)
{
if ($smod==$bbuserinfo['usergroupid'])
{
$usersmod=1;
}
}
}

if($admgroups[0])
{
foreach($admgroups as $adm)
{
if ($adm==$userinfo['usergroupid'])
{
$postadmin=1;
}
}
}
if($modgroups[0])
{
foreach($modgroups as $mod)
{
if ($mod==$userinfo['usergroupid'])
{
$postmod=1;
}
}
}
if($smodgroups[0])
{
foreach($smodgroups as $smod)
{
if ($smod==$userinfo['usergroupid'])
{
$postsmod=1;
}
}
}


could be replaced with
$useradmin = preg_match("/^(.*|)?$bbuserinfo[usergroupid](|.*)?$/",$vboptions['warn_admingroups']);
$postadmin = preg_match("/^(.*|)?$post[usergroupid](|.*)?$/",$vboptions['warn_admingroups']);

$usermod = preg_match("/^(.*|)?$bbuserinfo[usergroupid](|.*)?$/",$vboptions['warn_modgroups']);
$postmod = preg_match("/^(.*|)?$post[usergroupid](|.*)?$/",$vboptions['warn_modgroups']);

$usersmod = preg_match("/^(.*|)?$bbuserinfo[usergroupid](|.*)?$/",$vboptions['warn_smodgroups']);
$postsmod = preg_match("/^(.*|)?$post[usergroupid](|.*)?$/",$vboptions['warn_smodgroups']);

There's exactly the same code fragment in member.php which should use the similar treatment.

sv1cec
04-28-2005, 12:37 PM
Thanks for the tip, I'll check it out.