0

I want to include a section of one html document (X) in another one (Y).

There's:

<div id="cmform">
....
....
</div>

in html document X and I need to include it in a div in html document Y. I started to read PHP documentation and have found an info about "file_get_contents" function. If I do:

<?php
   $a = file_get_contents("http://site.com/document.html");
   echo ($a);
?>

I get the whole page and I don't know how to... narrow it to that one div...

Maria
  • 1

4 Answers4

0

You can use the DOMDocument class in PHP.

Documentation:
http://php.net/manual/en/class.domdocument.php

Example:

$documentText = file_get_contents("http://site.com/document.html");
$domDocument = new DOMDocument();
$domDocument->loadHTML( $documentText );
$myDivNode = $domDocument->getElementById( 'the-id-of-the-div' );
$myDivText = $domDocument->saveHTML( $myDivNode );
echo $myDivText;
John Severinson
  • 5,145
  • 1
  • 16
  • 6
0

You have two ways to do it:

  1. If the remote site has CORS headers (Access-Control-Allow-Origin: *), you can inject it using an AJAX request; Please note that this will fail on browsers that do not support CORS
  2. You can parse it on the server

Option 2 is by far your preferred approach and relies on two libraries (three if you're like me): curl, which will handle the HTTP request, and DOMDocument, which handles the parsing.

I wrote a parser for someone a while back. You can find it there: https://stackoverflow.com/a/16144603/2167834 . It has a lot of detailed explanations on how to go through the DOM using DOMDocument.

Please note that DOMDocument is especially prone to breaking on the following:

  1. Incorrect charset definitions
  2. Broken HTML
  3. Inline JavaScript

You can, however, rewrite your source to deal with this.

In your case, once you have the DOMDocument and DOMXPath object, you want to query("//[@id=\"cmform\"]"). The two forward slashes mean "any parent", [@id is an exact match on the parameter id.

Note that **this will fail if the DOM document has multiple elements with the same ID. They shouldn't have, by HTML spec.

Community
  • 1
  • 1
Sébastien Renauld
  • 19,203
  • 2
  • 46
  • 66
0

Actually file_get_contents() returns an string representig the content of the file, the simplest way you have is to parse the string to get the value of the div, check this example:

$html = 'hola <div id="cmform">'.
            "the content of div".
            "</div>adios";

echo preg_replace('/(.+)<div id="cmform">(.+)<\/div>(.+)/m', '\2', $html);
eleuteron
  • 1,863
  • 20
  • 26
-1

You want to use something like .load function in jquery

underscore
  • 6,495
  • 6
  • 39
  • 78