Solved

Retrieving DIV content by ID

Posted on 2009-04-13
4
759 Views
Last Modified: 2013-12-12
Hi guys is there a way to retrieve the content of a certain DIV from a website? I want that when a form is filled in my form my PHP script retrieve the contents of a DIV with a certain ID from another site, is that possible, and if it is; How would I go about it?
0
Comment
Question by:doRodrigo
  • 2
4 Comments
 
LVL 9

Expert Comment

by:dan_neal
ID: 24134467
Depending on the content of the DIV you would want to call either the innerHTML or innerText for the div.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 24134632
>>is there a way to retrieve the content of a certain DIV from a website?

from a different domain name? I guess you will get a Permission Denied error if you try to retrieve other site's content using javascript XMLHTTP request, but certainly I'm not sure other methods available in php whether can accomplish this or not.
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24135797
Scrape is the technical terminology here
Scrape DIV in this case

Disclaimer: Don't scrape if you don't have permission
</php
 

$url = "http://www.theremotesitehere.com/page.html";
 

$raw = file_get_contents($url);
 

$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
 

$content = str_replace($newlines, "", html_entity_decode($raw));
 

$start = strpos($content,'<div class="unique name here"');
 

$end = strpos($content,'</div>',$start) + 6; // add 6 becuase of </div> is 6 chars long
 

$div = substr($content,$start,$end-$start);
 

// you can go further and extract inside your div as you wish now

// by accessing $div
 

?>

Open in new window

0
 
LVL 19

Accepted Solution

by:
NerdsOfTech earned 500 total points
ID: 24135880
oops <?php

;)
<?php

 

$url = "http://www.theremotesitehere.com/page.html";

 

$raw = file_get_contents($url);

 

$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");

 

$content = str_replace($newlines, "", html_entity_decode($raw));

 

$start = strpos($content,'<div class="unique name here"');

 

$end = strpos($content,'</div>',$start) + 6; // add 6 becuase of </div> is 6 chars long

 

$div = substr($content,$start,$end-$start);

 

// you can go further and extract inside your div as you wish now

// by accessing $div

 

?>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to use a function in heredoc 5 23
Before I get too far.. best way to save dates data 4 16
Centered Image 2 23
Bootstrap on PLNKR 3 14
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now