Possible to only load specific div of a website?

36,313

You won't be able to manipulate the URL to get only a portion of the page. So what you'll want to do is grab the page contents via the server-side language of your choice and then parse the HTML. From there you can grab the specific DIV you are looking for and then print that out to your screen. You could also use to remove unwanted content.

With PHP you could use file_get_contents() to read the file you want to parse and then use DOMDocument to parse it and grab the DIV you want.

Here's the basic idea. This is untested but should point you in the right direction:

$page = file_get_contents('http://touch.facebook.com');
$doc = new DOMDocument();
$doc->loadHTML($page);
$divs = $doc->getElementsByTagName('div');
foreach($divs as $div) {
    // Loop through the DIVs looking for one withan id of "content"
    // Then echo out its contents (pardon the pun)
    if ($div->getAttribute('id') === 'content') {
         echo $div->nodeValue;
    }
}
Share:
36,313

Related videos on Youtube

brybam
Author by

brybam

Brinkbit.com

Updated on September 17, 2022

Comments

  • brybam
    brybam over 1 year

    I have this project I'm working on and id like to add a really small list of nearby places using Facebook's places in an iframe feature from touch.facebook.com. I can easily just use touch.facebook.com/#places.php but then that loads the top bars.

    Anyway, is there a way I could manipulate the URL so that it will only load the places content rather than having the top and bottom menu bars. After looking at the code it looks like there is a div id="content". I was thinking maybe there was a way to make the URL so that it will only load that div? I also tried making a URL that might just jump down to content using touch.facebook.com/#places.php#content but apparently with that way touch.facebook.com was built that doesn't work.

    I'd really rather figure out a way to just load only the section of content.

    I went ahead and tried this approach in PHP. I read the examples for file_get_contents and what's being here looks right, according to example 1:

    <?php
    $page = file_get_contents('http://touch.facebook.com');
    $doc = new DOMDocument();
    $doc->loadHTML($page);
    $divs = $doc->getElementsByTagName('div');
    foreach($divs as $div) {
          if ($div->getAttribute('id') === 'content') {
             echo $div->nodeValue;
        }
    }
    ?>
    

    But, I get a completely blank page.

  • brybam
    brybam over 13 years
    I have seen it done before. The content div of the main page contains the login area, then after the user is authenticated i can have them redirected to the next page that contains another content div which is all i want to display. I really only have to worry about the authentication once, because i can keep them logged in. I really don't want to go into it programmatically since it's such a small thing im trying to do i figure it wouldnt be worth the time. and ive seen what im trying to figure out done before so i know it can be.
  • brybam
    brybam over 13 years
    I went ahead and tried what you said, but it looks like there is an error with file_get_contents() is there another way it should be used?
  • John Conde
    John Conde over 13 years
    Your PHP must be set to allow URL fopen (it should be set to 1)
  • brybam
    brybam over 13 years
    is this something that's defined in my .php document? Or will i have to contact my server host company about my php configuration? Also, I caught a typo I made, and adjusted the first post. Thanks a lot for all the insight on this topic. Like I said, really new to php.
  • John Conde
    John Conde over 13 years
    Looks like you will need to contact your host as it seems it can only be changed in the php.ini file: php.net/manual/en/…
  • brybam
    brybam over 13 years
    I've contacted my server company and confirmed the php.ini is configured properly. Now the only issue im having is just a blank page.