preg match to get content from another site

Discussion in 'PHP' started by webber09, Apr 8, 2011.

  1. #1
    hi all,

    i am looking to have the latest tech news on my site but want it to update from a website called http://www.techjunkeez.com

    i know you can do it using the
    <?php
    $data = file_get_contents("website where info is");
    preg_match_all('content wanted', 
    $data, 
    $result);
    $num_elements = count($result);
    ?>
    PHP:
    then using
    <?php
    for($i = 0; $i < 40; $i++) {
    echo "content here";
    }
    ?>
    PHP:
    functions.

    the info i want from the site is the top tech story of the day (today its the section about windows 8).

    <p class="t1" style="text-align: center">Windows 8 Screenshots Reveal Ribbon  Interface</td></tr></table></td></tr><tr><td><table width="100%" border="0" cellpadding="5" cellspacing="0" bgcolor="#EFEFEF"><tr><td bgcolor="#EFEFEF" valign="top">
    <p class="x2"><img border="1" src="images/windows72.jpg" align="right" hspace="9" vspace="6" width="190" height="149">Images from a pre-beta version of Windows 8 reveal that Microsoft is apparently expanding its use of the ribbon interface to replace traditional pull-down menus and toolbars.</p><p class="x2">Following Microsoft's release of the latest pre-beta build of Windows 8 to select partners recently, screenshots of certain features in the upcoming OS were allegedly posted online. In particular, images displayed on enthusiast site Within Windows by &quot;Windows 8 Secrets&quot; co-authors Rafael Rivera and Paul Thurrott show the ribbon interface reaching Windows Explorer.</p><p class="x2">Introduced with Office 2007, the ribbon interface has been loved by some and reviled by others. But Microsoft has expanded its use of the ribbon, adding it to such programs as Paint and WordPad in Windows 7. Noting that the ribbon interface in the pre-beta Windows 8 is only half-finished and rather &quot;unattractive,&quot; Rivera and Thurrott suggest that its potential appearance in Explorer may be controversial within Microsoft itself.</p><p class="x2">Beyond showing off the ribbon, the two authors also revealed a new welcome screen in Windows 8, which they say is based on the lock screen in Windows Phone 7. Displaying the date and time and offering a changeable background image, the new screen reportedly will also feature audio controls so people can play and adjust their music even when the screen is locked.</p>
    
    HTML:
    could anyone please advise how to do this, thanks :D
     
    webber09, Apr 8, 2011 IP
  2. _:codefan:_

    _:codefan:_ Active Member

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Download Simple HTML DOM

    And the code would be like this (not tested) :
    
    require '/simple_html_dom.php';
    
    $html = file_get_html('http://xxxxxx.com');
    
    foreach($html->find('p.x2') as $element) {
       echo $element->innertext();
       break;
    }	
    
    PHP:
     
    _:codefan:_, Apr 8, 2011 IP
  3. webber09

    webber09 Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #3
    the above does work however it only gets the first x2 paragraph. is there a way of getting this code to get ALL of the x2's without knowing how many there are each day? also the image doesnt link properly due to it being a relative link on the site, is there a way to put the http://www.techjunkeez.com into the src in the php code?
     
    Last edited: Apr 9, 2011
    webber09, Apr 9, 2011 IP
  4. _:codefan:_

    _:codefan:_ Active Member

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    58
    #4
    remove break; from the loop :)
     
    _:codefan:_, Apr 9, 2011 IP
  5. webber09

    webber09 Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #5
    all done, bit ruff but it works but this is how iv finished up with it :D

    <?php
    require 'simple_html_dom.php';
    
    $html = file_get_html('http://www.techjunkeez.com');
    
    echo '<p align="center">';
    foreach($html->find('p.t1') as $title) {
       echo $title->innertext();
       break;
    }
    echo "</p>";
    
    foreach($html->find('p.x2') as $info) {
    $findsrc = array('height="100">', 'align="right"', 'border="1" src="');
    $changesrc   = array('height="100"><p>', 'align="center"', 'border="0" src="http://www.techjunkeez.com/');
    $output1 = str_replace($findsrc, $changesrc, $info);
    echo $output1;
    break;
    }
    echo "</p>";
    
    echo "<p>";
    foreach($html->find('p.x3') as $link) {
       echo $link->innertext();
       break;
    }
    echo "</p>";
    
    ?>
    PHP:
     
    webber09, Apr 9, 2011 IP