I was hoping to parse a webpage and extract all link and image URLs
from it and enter the new ones into mySQL tables, below is my code to
do it, I've tried to optimise it as much as I can but it still takes
too long to execute (server timeouts on a server which I can not
control) I was wondering if there was some way to compile the code or
if anyone can spot something which could be better written.
Thanks in advance,
Martin
<?
$start=microtim e_float();
$uid=$_GET['uid'];
$restriction= "dontstayin ";
include("loadst uff.php"); //just contains a function which echos a tick
or cross
include("userpa ss.php"); //contains database access details.
mysql_connect($ host,$user,$pas sword);
@mysql_select_d b("$database" ) or die(cross());
$result = mysql_query("SE LECT * FROM mark_toparse WHERE uid='$uid'");
//mysql_close();
$contents = mysql_result($r esult,0,"fconte nts");
$fullorig = mysql_result($r esult,0,"origin alpath");
$origpath = substr($origpat h,0,strrpos($fu llorig,"/")+1);
$lines = explode(">",$co ntents);
for($i=0;$i<cou nt($lines);$i++ ){
$imgsrc = stristr($lines[$i],"<img");
if($imgsrc!=fal se){
$imgsrc = str_replace("'" ,"",$imgsrc) ;
$f = strpos($imgsrc, "\"",strpos($im gsrc,"src"));
$l = strpos($imgsrc, "\"",$f+1);
$url = substr($imgsrc, $f+1,$l-$f-1);
if(strncasecmp( $url,"http:",5) !=0){
$url = $origpath . $url;
$url= str_replace(":/","://",str_repla ce("//","/",$url));
}
$resone = mysql_query("SE LECT count(*) FROM mark_images WHERE
url='$url'");
$a = (mysql_fetch_ar ray($resone,MYS QL_NUM));
if($a[0]==0){
mysql_query("IN SERT INTO mark_images VALUES('','$url ')");
}
}else{
$link = stristr($lines[$i],"href=");
if($link!=false ){
$link = str_replace("'" ,"",$link);
$f = strpos($link,"\ "");
$l = strpos($link,"\ "",$f+1);
$url= substr($link,$f +1,$l-$f-1);
if(strncasecmp( $url,"http:",5) !=0){
if(strncasecmp( $url,"mailto:", 7)==0||strncase cmp($url,"ftp:" ,4)==0||strncas ecmp($url,"msni m:",6)==0){
//ignore ftp, mailto and msn links
}else{
$url = $origpath . $url;
$url= str_replace(":/","://",str_repla ce("//","/",$url));
if(eregi("\.jp[eg2]{1,2}$",$url)){
$resone = mysql_query("SE LECT count(*) FROM mark_images WHERE
url='$url'");
$a = (mysql_fetch_ar ray($resone,MYS QL_NUM));
if($a[0]==0){
mysql_query("IN SERT INTO mark_images VALUES('','$url ')");
}
}else{
$resone = mysql_query("SE LECT count(*) FROM mark_linktodl WHERE
url='$url'");
$a = (mysql_fetch_ar ray($resone,MYS QL_NUM));
if($a[0]==0){
mysql_query("IN SERT INTO mark_linktodl VALUES('','$url ')");
}
}
}
}elseif(eregi($ restriction,$ur l)){
if(eregi("\.jp[eg2]{1,2}$",$url)){
$resone = mysql_query("SE LECT count(*) FROM mark_images WHERE
url='$url'");
$a = (mysql_fetch_ar ray($resone,MYS QL_NUM));
if($a[0]==0){
mysql_query("IN SERT INTO mark_images VALUES('','$url ')");
}
}else{
$resone = mysql_query("SE LECT count(*) FROM mark_linktodl WHERE
url='$url'");
$a = (mysql_fetch_ar ray($resone,MYS QL_NUM));
if($a[0]==0){
mysql_query("IN SERT INTO mark_linktodl VALUES('','$url ')");
}
}
}
}
}
}
//mysql_query("DE LETE FROM mark_toparse WHERE
originalpath='$ fullorig'");
mysql_query("IN SERT INTO mark_parsed
VALUES('','$ful lorig','".md5($ contents)."')") ;
mysql_close();
$finish=microti me_float();
if(strcmp($_GET['debug'],"t")==0){
$tim=$finish-$start;
include("error_ image.php");
echo imagepng(errori mage("Analysis: $tim s"));
}else{
echo tick();
}
function microtime_float ()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
?>
from it and enter the new ones into mySQL tables, below is my code to
do it, I've tried to optimise it as much as I can but it still takes
too long to execute (server timeouts on a server which I can not
control) I was wondering if there was some way to compile the code or
if anyone can spot something which could be better written.
Thanks in advance,
Martin
<?
$start=microtim e_float();
$uid=$_GET['uid'];
$restriction= "dontstayin ";
include("loadst uff.php"); //just contains a function which echos a tick
or cross
include("userpa ss.php"); //contains database access details.
mysql_connect($ host,$user,$pas sword);
@mysql_select_d b("$database" ) or die(cross());
$result = mysql_query("SE LECT * FROM mark_toparse WHERE uid='$uid'");
//mysql_close();
$contents = mysql_result($r esult,0,"fconte nts");
$fullorig = mysql_result($r esult,0,"origin alpath");
$origpath = substr($origpat h,0,strrpos($fu llorig,"/")+1);
$lines = explode(">",$co ntents);
for($i=0;$i<cou nt($lines);$i++ ){
$imgsrc = stristr($lines[$i],"<img");
if($imgsrc!=fal se){
$imgsrc = str_replace("'" ,"",$imgsrc) ;
$f = strpos($imgsrc, "\"",strpos($im gsrc,"src"));
$l = strpos($imgsrc, "\"",$f+1);
$url = substr($imgsrc, $f+1,$l-$f-1);
if(strncasecmp( $url,"http:",5) !=0){
$url = $origpath . $url;
$url= str_replace(":/","://",str_repla ce("//","/",$url));
}
$resone = mysql_query("SE LECT count(*) FROM mark_images WHERE
url='$url'");
$a = (mysql_fetch_ar ray($resone,MYS QL_NUM));
if($a[0]==0){
mysql_query("IN SERT INTO mark_images VALUES('','$url ')");
}
}else{
$link = stristr($lines[$i],"href=");
if($link!=false ){
$link = str_replace("'" ,"",$link);
$f = strpos($link,"\ "");
$l = strpos($link,"\ "",$f+1);
$url= substr($link,$f +1,$l-$f-1);
if(strncasecmp( $url,"http:",5) !=0){
if(strncasecmp( $url,"mailto:", 7)==0||strncase cmp($url,"ftp:" ,4)==0||strncas ecmp($url,"msni m:",6)==0){
//ignore ftp, mailto and msn links
}else{
$url = $origpath . $url;
$url= str_replace(":/","://",str_repla ce("//","/",$url));
if(eregi("\.jp[eg2]{1,2}$",$url)){
$resone = mysql_query("SE LECT count(*) FROM mark_images WHERE
url='$url'");
$a = (mysql_fetch_ar ray($resone,MYS QL_NUM));
if($a[0]==0){
mysql_query("IN SERT INTO mark_images VALUES('','$url ')");
}
}else{
$resone = mysql_query("SE LECT count(*) FROM mark_linktodl WHERE
url='$url'");
$a = (mysql_fetch_ar ray($resone,MYS QL_NUM));
if($a[0]==0){
mysql_query("IN SERT INTO mark_linktodl VALUES('','$url ')");
}
}
}
}elseif(eregi($ restriction,$ur l)){
if(eregi("\.jp[eg2]{1,2}$",$url)){
$resone = mysql_query("SE LECT count(*) FROM mark_images WHERE
url='$url'");
$a = (mysql_fetch_ar ray($resone,MYS QL_NUM));
if($a[0]==0){
mysql_query("IN SERT INTO mark_images VALUES('','$url ')");
}
}else{
$resone = mysql_query("SE LECT count(*) FROM mark_linktodl WHERE
url='$url'");
$a = (mysql_fetch_ar ray($resone,MYS QL_NUM));
if($a[0]==0){
mysql_query("IN SERT INTO mark_linktodl VALUES('','$url ')");
}
}
}
}
}
}
//mysql_query("DE LETE FROM mark_toparse WHERE
originalpath='$ fullorig'");
mysql_query("IN SERT INTO mark_parsed
VALUES('','$ful lorig','".md5($ contents)."')") ;
mysql_close();
$finish=microti me_float();
if(strcmp($_GET['debug'],"t")==0){
$tim=$finish-$start;
include("error_ image.php");
echo imagepng(errori mage("Analysis: $tim s"));
}else{
echo tick();
}
function microtime_float ()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
?>
Comment