0

I was wondering, is it possible to get specific data from another website ? Let's say i want from page X the content from a div. How can i make write in php a script that automaticaly gets and echo into my site what is inside that div from page X.

3
Contributors
3
Replies
9
Views
7 Years
Discussion Span
Last Post by JRM
0

Though I have never used these functions before, apparently PHP has had support for manipulating DOM elements for a while now. For example:

http://se.php.net/manual/en/domdocument.getelementbyid.php

You should be able to use that function to get the content of the div based on its ID. There are also other functions to get the content based on other attributes in case it doesn't have an ID. You can see all the functions here:

http://se.php.net/dom

You can also use a regular expression instead of all of this. This is probably what I would do because I am used to working with regular expressions and haven't used the above functions yet.

In either of these cases you are going to first use something like file_get_contents() to retrieve the remote file and put its data in a variable.

For example:

$data = file_get_contents('http://example.com/test.html');
$divData = preg_replace("/(.*?)\<div\>(.*?)\<\/div\>(.*)/", "$2", $data);

Of course you'd probably have to do a bit more with the regular expression to match only the div you want, based on some attribute only it has, but that is the general idea.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.