0

So, say I have my website:

<div id="MySiteDIV">
 <!-- Content in here -->
</div>

And the other persons website:

<div id="CTs_g">
 <p>Hello</p>
  <div id="AG_g">This is my website</div>
</div>

How do I get the HTML and all so on my website it would end up looking like:

<div id="MySiteDIV">
 <!-- Content in here -->

<div id="CTs_g">
 <p>Hello</p>
  <div id="AG_g">This is my website</div>
</div>

</div>

I've seen (and tried) the DOMDocument() but it seemed to only get me the contents but I need the HTML.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088

2 Answers2

1

Here you go:

$dom = new DOMDocument();
$yourDom = new DOMDocument();
$yourDom->loadHTML(file_get_contents('your_template'));
$dom->loadHTML(file_get_contents('other_website'));

// You can now use the DOMDocument methods to parse whatever you want.
$div = $dom->getElementById("CTs_g");
$clone = $div->cloneNode(true);
$yourDiv = $yourDom->getElementById("MySiteDIV");
$yourDiv->appendChild($clone);
om_deshpande
  • 665
  • 1
  • 5
  • 16
0

As @Mohammad mentions you will need to use php for this. You will first grab the remote page with file_get_contents:

$html = file_get_contents('http://othersite.com');

Then parse that with either preg_match (regular expression) or some 3rd party library that does page scraping such as https://code.google.com/p/phpquery/PHPQuery

Using preg_match is way easier but you need to be able to define the beginning/end of the desired section. If you know exactly - or ideally if you have control over the other site in which case I would add begin/end comment tags - then great else you might need to run the entire page through php query which wont be easy as far as finding the right section.

In your above case it's easy enough to define the start (i.e. id="MySiteDIV") but end is much trickier. So my recommendation is to find 2 parts in that remote site that are unique and use preg_match to grab that. Welcome to regex.

cyberwombat
  • 38,105
  • 35
  • 175
  • 251
  • 1
    See accepted answer here: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags for why it is a bad idea to use regex for this. – Luke Mills Dec 31 '13 at 03:39