2017-02-19 3 views
0

私のWebクローラ抽出文に前述の "テキスト"を追加しようとしています。PHPトップ1から5の文に上記の "テキスト"を含むランダム

PHPのWebクローラー::

<?php 
    function get_data($url) { 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_URL,$url); 
     $result = curl_exec($ch); 
     curl_close($ch); 
     return $result; 
    } 
    $returned_content = get_data('http://www.usmle-forums.com/usmle-step-1-forum/'); 
    $first_step = explode('<tbody id="threadbits_forum_26"' , $returned_content); 
    $second_step = explode('</tbody>', $first_step[1]); 
    $third_step = explode('<tr>', $second_step[0]); 
    // print_r($third_step); 
    foreach ($third_step as $key=>$element) { 
    $child_first = explode('<td class="alt1"' , $element); 
    $child_second = explode('</td>' , $child_first[1]); 
    $child_third = explode('<a href=' , $child_second[0]); 
    $child_fourth = explode('</a>' , $child_third[1]); 
    $final = "<a href=".$child_fourth[0]."</a></br>"; 
?> 
    <li target="_blank" class="itemtitle"> 
     <span class="item_new">text</span><?php echo $final?> 
    </li> 
<?php 
    if($key==10){ 
     break; 
    } 
} 
?> 

は今、私は、Webクローラーによって抽出された最初の5つの文にランダムに追加するspanタグに記載された「テキスト」を挿入しようとして。

このように、ユーザーがページを更新するたびに、スパンタグの「テキスト」が上位5つのステートメントの間でシャッフルする必要があります。

すべてのヘルプは高く評価され..

+0

この質問は、追加の明瞭さを必要とします。あなたは何を試みましたか?現在の出力は何ですか?意図する出力は何ですか?何が適切に機能していますか?何が失敗していますか? – mickmackusa

答えて

1

これはあなたが望むものであるかどうかわからない:

<?php 
    function get_data($url) { 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_URL,$url); 
    $result=curl_exec($ch); 
    curl_close($ch); 
    return $result; 
    } 
    $returned_content = get_data('http://www.usmle-forums.com/usmle-step-1-forum/'); 
    $first_step = explode('<tbody id="threadbits_forum_26"' , $returned_content); 
    $second_step = explode('</tbody>', $first_step[1]); 
    $third_step = explode('<tr>', $second_step[0]); 
    // print_r($third_step); 
    foreach ($third_step as $key=>$element) { 
    $child_first = explode('<td class="alt1"' , $element); 
    $child_second = explode('</td>' , $child_first[1]); 
    $child_third = explode('<a href=' , $child_second[0]); 
    $child_fourth = explode('</a>' , $child_third[1]); 
    $final = "<a href=".$child_fourth[0]."</a></br>"; 

    echo '<li target="_blank" class="itemtitle">'; 
    if($key < 5 && rand(0,1) == 1) { 
     echo '<span class="item_new">YOUR TEXT </span>'; 
    } 

    echo $final; 
    echo '</li>'; 

    if($key==10) { 
     break; 
    } 

    } 
?> 

それは最初の5行にランダムに「YOUR TEXT」を追加します。

出力:

YOUR TEXT 
1 Attachment(s) 
Pass Your Step 1 Exam - 100% Guaranteed! 
USMLE Step 1 Live prep/Boot Camp in Chicago 
YOUR TEXT 1 Attachment(s) 
Picmonic for Medicine 
Increase Your Step 1 Score Through 1-on-1 Online Tutoring 
Low Score on Step 1? 
step 1 tutoring 
Frustrated USMLE taker mom. 
Looking for a tutor 
+0

おい、ちょうど私が必要なものだが、このコードは動作していない..それはすべての10文の "テキスト"を示している.. – harishk

+0

@harishk:それはうまく動作する。最初の5行に無作為に追加されたテキスト "text"が見えます。予想される結果の例で質問を更新できますか?また、私が投稿したソリューションを更新しました。 – uautkarsh

関連する問題