Problem with array

Discussion in 'PHP' started by ssimon171078, Nov 4, 2014.

  1. #1
    i need to print each element in array ,i wrote code in php .i need that in line : echo $line . "<br />"; i see each element in array but i see a problem convert it to string.

    <?php
    $website="https://www.tradebit.com/filesharing.php/1010-Documents-eBooks-Audio-Books-Teaching";
    
    //echo $website ;
    $content=file_get_contents($website);
    
    preg_match_all("/<a href=\"([^\"]*)\">(.*)<\/a>/iU",$content,$result);
    
    //print_r($result);
    foreach ($result as $line ){
    echo  $line . "<br />";
    }
    ?>
    Code (markup):
     
    Last edited by a moderator: Nov 4, 2014
    ssimon171078, Nov 4, 2014 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,895
    Likes Received:
    4,553
    Best Answers:
    123
    Trophy Points:
    665
    #2
    what you have should be fine but you could try something like this too

    foreach($result as $line){
       echo "{$line}<br />";
    }
    Code (markup):
    The docs for preg_match_all state that $result[1] will actually hold the information that you need - but in another array

    so your code should be
    foreach($result[1] as $line){
       echo "{$line}<br />";
    }
    Code (markup):
    and if you want to be really tricky you could use

    echo implode("<br />", $result[1]);
    Code (markup):
     
    sarahk, Nov 4, 2014 IP