2012-06-26 59 views
0

好的,我在一個不太清楚的問題發佈之前,讓我再試一次。 我試圖組織我的網站批處理,每批5個站點都有不同的IP地址。php按url排序的網址

要做到這一點,我必須爲每個網址實時獲取IP,然後將它們(站點)組織成每批5個站點,每個站點都有自己的唯一IP地址。

如果我有多個IP地址,它們必須顯示在下一批中。

任何人都可以幫助我解決這個問題嗎?

我有一個數組與網站和IP。

這裏是我的代碼:

if(isset($Organize_List)){ 
$List = explode("\n", $Organize_List); 
$IP_Array = array(); 
     foreach($List as $SIte){ 
    $Ip = gethostbyname(trim($SIte)); 
     if(preg_match('/^\d+$/', $Ip[1])){ 
     $IP_Array[$Ip][] = $SIte.'<br />'; 
       } 
} 

現在,這裏是它得到的棘手。

我的問題是如何將組織成每批5個站點的批次,每個站點都有自己的唯一IP地址。

+2

'parse_url','gethostbyname','foreach'。足以讓你開始,如果遇到任何與你的實際代碼有關的問題,請回來。 – Wrikken

+0

我已經完成了所有的工作,我的問題是沒有獲得每個網站的信息,但是,將它們全部對準我上面提到的莊園。 – RmH

+0

添加你的代碼.. – sarnold

回答

0
/* your base data */ 
$domains = array(/* your data */); 

/* a list of domains per ip number, like $domain_by_ip['64.34.119.12'] = 'stackoverflow.com' */ 
$domain_by_ip = array(); 

/* a list counting number of domains by ip number */ 
$ip_count = array(); 

/* a list of domains we faild to fetch ip number for */ 
$failed = array(); 

/* loop through all doains */ 
foreach($domains as $current_domain) 
{ 
    /* fetch the A record for all domains */ 
    $current_dns_record = dns_get_record($current_domain, DNS_A); 

    /* if there is a dns record */ 
    if($current_dns_record) 
    { 
     /* fetch ip from result */ 
     $current_ip = $current_dns_record[0]['ip']; 

     /* thos row is not needed, but php may triggering a warning oterhwise */ 
     if(!isset($domain_by_ip[$current_ip])) $domain_by_ip[$current_ip] = array(); 
     if(!isset$ip_count[$current_ip])) $ip_count[$current_ip] = array(); 

     /* add domain to the list by ip */ 
     $domain_by_ip[$current_ip][] = $current_dns_record; 

     /* count up the count of domains on this ip */ 
     $ip_count[$current_ip]++; 
    } 
    else 
    { 
     /* if there was no dns record, put this domain on the fail list */ 
     $failed[] = $current_domain; 
    } 
} 

/* create a list for storing batches */ 
$batches = array(); 

/* as long as we have ip-numbers left to use */ 
while($ip_count) 
{ 
    /* create a list for storing current batch */ 
    $current_batch = array(); 

    /* sort ip-numbers so we take the ip-numbers whit most domains first */ 
    arsort($ip_count); 

    /* take the top 5 ip-numbers from the list */ 
    $current_batch_ip_list = array_slice(array_keys($ip_count), 0, 5); 

    /* foreach of thous 5 ip-numbers .. */ 
    foreach($current_batch_ip_list as $current_ip) 
    { 
     /* move one domain from the domain by ip list to the current batch */ 
     $current_batch[] = array_pop($domain_by_ip[$current_ip]); 

     /* count down the numbers of domains left for that ip */ 
     $ip_count[$current_ip]--; 

     /* if there is no more domains on this ip, remove it from the list */ 
     if($ip_count[$current_ip] == 0) 
     { 
     unset($ip_count[$current_ip]); 
     } 
    } 

    /* add current batch to the list of batches */ 
    $batches[] = $current_batch; 
} 
+0

首先感謝您快速重播。代碼彈出關閉這個錯誤,雖然:「Dns查詢失敗」這將工作都一樣,如果我嘗試使用它與gethostbyname? – RmH

+0

dns是我用來獲取ip-adresses的方式,gethostbyname應該工作,只需替換'$ current_dns_record =' - 行和'$ current_ip =' - 行 –

+0

好的編輯。所有這一切它彈出關閉此味精: 未定義的指數:199.96.156.205 – RmH