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.

Recommended Answers

All 3 Replies

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.

10x i get the idea

PHP also has cURL methods that may work for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.