PHP如何优化脚本?

时间:2011-06-05 10:54:03

标签: php xml optimization xml-parsing


这个脚本在我看来太长了。有没有办法优化它?

function delteg($string){
$keyp=0;
$out="";
   for ($i=0; $i<strlen($string); $i++) { 
           if ($string[$i]=="<"):
             $keyp=1;
           endif;

           if ($keyp==0):
             $out.=$string[$i];
           endif;

           if ($string[$i]==">"):
             $keyp=0;
           endif;

   }
   $out = substr($out,0,strlen($out));
   return $out;
}

$time_step = array();
$datetime = array();
$G = array();
$HHii = array();
$cloud_cover = array();
$precipitation = array();
$pressure = array();
$temperature = array();
$humidity = array();
$wind_direction = array();
$wind_velocity = array();
$falls = array();
$drops = array();
$n = 0;

$handle = fopen ("http://rp5.ua/xml/257885/en", "r");
while (!feof ($handle)) {
$buffer = fgets($handle, 4096); 

 if (strpos($buffer, "country_id")>0) ($country_id[$n]=delteg($buffer)); 
 if (strpos($buffer, "point_name")>0) ($point_name[$n]=delteg($buffer));
 if (strpos($buffer, "point_name_trim")>0) ($point_name_trim[$n]=delteg($buffer)); 
 if (strpos($buffer, "point_name2")>0) ($point_name2[$n]=delteg($buffer));
 if (strpos($buffer, "point_timestamp")>0) ($point_timestamp[$n]=delteg($buffer)); 
 if (strpos($buffer, "gmt_add")>0) ($gmt_add[$n]=delteg($buffer)); 
 if (strpos($buffer, "point_date")>0) ($point_date[$n]=delteg($buffer));
 if (strpos($buffer, "point_date_time")>0) ($point_date_time[$n]=delteg($buffer)); 
 if (strpos($buffer, "time_step")>0) ($time_step[$n]=delteg($buffer)); 
 if (strpos($buffer, "datetime")>0) ($datetime[$n]=delteg($buffer));
 if (strpos($buffer, "G")>0) ($G[$n]=delteg($buffer)); 
 if (strpos($buffer, "HHii")>0) ($HHii[$n]=delteg($buffer));
 if (strpos($buffer, "cloud_cover")>0) ($cloud_cover[$n]=delteg($buffer)); 
 if (strpos($buffer, "precipitation")>0) ($precipitation[$n]=delteg($buffer));
 if (strpos($buffer, "pressure")>0) ($pressure[$n]=delteg($buffer)); 
 if (strpos($buffer, "temperature")>0) ($temperature[$n]=delteg($buffer));
 if (strpos($buffer, "humidity")>0) ($humidity[$n]=delteg($buffer)); 
 if (strpos($buffer, "wind_direction")>0) ($wind_direction[$n]=delteg($buffer));
 if (strpos($buffer, "wind_velocity")>0) ($wind_velocity[$n]=delteg($buffer)); 
 if (strpos($buffer, "falls")>0) ($falls[$n]=delteg($buffer));
 if (strpos($buffer, "drops")>0) :($drops[$n]=delteg($buffer));
   $n++;
 endif;

}

fclose ($handle);

for ($i=0; $i<$n; $i++) {
     echo $country_id[$i]." - ".$point_name[$i]. " etc.....";

}

3 个答案:

答案 0 :(得分:4)

当然。您应该使用XML解析器,例如SimpleXML

示例:

$xml = simplexml_load_file("http://rp5.ua/xml/257885/en");

foreach($xml->point as $point) {
    echo 'Point: ', $point->point_name, PHP_EOL;
    foreach($point->timestep as $step) {
        echo "\t Time: ", $step->datetime, PHP_EOL;
    }
}

没有理由手动解析XML。如果使用解析器,可以省去很多麻烦。

答案 1 :(得分:0)

有些人可能会说使用单引号作为字符串而不是双引号,因为PHP会在使用双精度时插入字符串。 Ilia有一些很好的一般PHP优化技术。 http://ilia.ws/archives/12-PHP-Optimization-Tricks.html

从我的观点来看,这一切看起来都很好我唯一要说的是,你是否有任何理由在for循环声明中调用strlen(),因为一旦将数字分配给它,调用它会更快变量并在for循环中使用它。

哦,是的并且使用XML解析器,带有一些回调的XML解析器对象可以工作,而simpleXML也很好。

答案 2 :(得分:0)

您的脚本存在许多问题。例如:

  • 您没有使用XML解析器而只是将其作为字符串处理(效率不高,基本上是错误的) - 例如使用SimpleXML
  • 您的代码多行 不需要,因为不要更改代码的结果 - 下面的示例解决方案,
  • 一致的方式编写代码(例如,使用缩进,对条件子句使用相同的表示法) - 不会真正影响执行速度,但会帮助您进行代码维护,< / LI>

问题出在这里:

if (strpos($buffer, "country_id")>0) ($country_id[$n]=delteg($buffer)); 
 if (strpos($buffer, "point_name")>0) ($point_name[$n]=delteg($buffer));
 if (strpos($buffer, "point_name_trim")>0) ($point_name_trim[$n]=delteg($buffer)); 
 if (strpos($buffer, "point_name2")>0) ($point_name2[$n]=delteg($buffer));
 if (strpos($buffer, "point_timestamp")>0) ($point_timestamp[$n]=delteg($buffer)); 
 if (strpos($buffer, "gmt_add")>0) ($gmt_add[$n]=delteg($buffer)); 
 if (strpos($buffer, "point_date")>0) ($point_date[$n]=delteg($buffer));
 if (strpos($buffer, "point_date_time")>0) ($point_date_time[$n]=delteg($buffer)); 
 if (strpos($buffer, "time_step")>0) ($time_step[$n]=delteg($buffer)); 
 if (strpos($buffer, "datetime")>0) ($datetime[$n]=delteg($buffer));
 if (strpos($buffer, "G")>0) ($G[$n]=delteg($buffer)); 
 if (strpos($buffer, "HHii")>0) ($HHii[$n]=delteg($buffer));
 if (strpos($buffer, "cloud_cover")>0) ($cloud_cover[$n]=delteg($buffer)); 
 if (strpos($buffer, "precipitation")>0) ($precipitation[$n]=delteg($buffer));
 if (strpos($buffer, "pressure")>0) ($pressure[$n]=delteg($buffer)); 
 if (strpos($buffer, "temperature")>0) ($temperature[$n]=delteg($buffer));
 if (strpos($buffer, "humidity")>0) ($humidity[$n]=delteg($buffer)); 
 if (strpos($buffer, "wind_direction")>0) ($wind_direction[$n]=delteg($buffer));
 if (strpos($buffer, "wind_velocity")>0) ($wind_velocity[$n]=delteg($buffer)); 
 if (strpos($buffer, "falls")>0) ($falls[$n]=delteg($buffer));
 if (strpos($buffer, "drops")>0) :($drops[$n]=delteg($buffer));
   $n++;
 endif;

你真的只需要前两行:

if (strpos($buffer, "country_id")>0) ($country_id[$n]=delteg($buffer)); 
if (strpos($buffer, "point_name")>0) ($point_name[$n]=delteg($buffer));

因为稍后你只使用它们生成的值。