2016-07-21 62 views
-4

我想創建變量$連接的陣列來獲得陣中的所有鏈接,這樣我可以花括號我如何在PHP中創建一個字符串對象的數組?

include("simple_html_dom.php"); 


$html = file_get_html($url); 

$i=0; 
$linkObjs = $html->find('h3.r a'); 
foreach ($linkObjs as $linkObj) 
{ 
    $title = trim($linkObj->plaintext); 
    $link = trim($linkObj->href); 

    //if it is not a direct link but url reference found inside it, then extract 
    if (!preg_match('/^https?/', $link) && preg_match('/q=(.+)&sa=/U', $link, $matches) && preg_match('/^https?/', $matches[1])) 
    { 
     $link = $matches[1]; 
    } else if (!preg_match('/^https?/', $link)) { // skip if it is not a valid link 
     continue; 
    } 

    $descr = $html->find('span.st',$i); // description is not a child element of H3 thereforce we use a counter and recheck. 
    $i++; 
} 
+0

如果您不知道答案,請停止downvoting。 – Azhar

+3

你還沒有解釋問題是什麼或發生了什麼(或不) - 這可能是爲什麼你的問題是downvoted – RamRaider

+2

@Azhar停止假設人們downvote一個問題,因爲他們不知道答案 –

回答

1

創建一個數組,推動比賽外同時處理它們。

include("simple_html_dom.php"); 


$html = file_get_html($url); 

$links = array(); 
$i=0; 
$linkObjs = $html->find('h3.r a'); 
foreach ($linkObjs as $linkObj) 
{ 
    $title = trim($linkObj->plaintext); 
    $link = trim($linkObj->href); 


    // if it is not a direct link but url reference found inside it, then extract 
if (!preg_match('/^https?/', $link) && preg_match('/q=(.+)&sa=/U', $link, $matches) && preg_match('/^https?/', $matches[1])) 
{ 
    array_push($links, $link);   
} else if (!preg_match('/^https?/', $link)) { // skip if it is not a valid link 
    continue; 
    } 

$descr = $html->find('span.st',$i); // description is not a child element of H3 thereforce we use a counter and recheck. 
$i++; 
} 
+0

謝謝..它的工作:) – Azhar

0

只是聲明瞭一個數組變量,並將其添加以後使用它。

循環之前,

$myLinks = []; 

而且,這條線剛過,

$link = $matches[1]; 
$myLinks[] = $link; 

現在,你可以使用數組$ myLinks,希望這是你需要的東西。

+0

謝謝:) @Robin – Azhar

+0

不客氣:) –

相關問題