2016-07-21 9 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

答えがわからない場合は、ダウンボウリングを停止してください。 – Azhar

+3

問題が何であるか、何が起きているのかを説明していない - それはおそらくあなたの質問がダウン投票された理由です。 – 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; 

、あなたは、これはあなたが必要なものだったホープ、配列$個人用リンクを使用することができます。

+0

ありがとう@Robin – Azhar

+0

あなたは大歓迎です:) –

関連する問題