3

I have a very complicated PHP file which generates a lot of HTML, but I'm interested in extracting only some of it (within the <div id="content"> element) to another file. The thing is, that element is generated by the PHP.

Is it possible to use PHP's native getElementById function to get the content element from another PHP file? Or even from within itself?

For example, something like this, but actually working:

$doc = new DomDocument;

// We need to validate our document before referring to the id
$doc->validateOnParse = true;
$doc->Load('file.php');

echo "The contents are: ".$doc->getElementById('content')->textContent;

I realize this would mean the PHP will have to be executed to generate the HTML, which could be why the answer is simply No, this is not possible and I also realize this would mean a lot of additional run time. I'm not worried about those at the moment, just if this is possible.

0

5 Answers 5

2

You can try something like this:

ob_start()
include 'file.php';
$html = ob_get_clean();

$doc = new DomDocument;

// We need to validate our document before referring to the id
$doc->validateOnParse = true;
$doc->loadHTML($html);
Sign up to request clarification or add additional context in comments.

1 Comment

Don't know why I did not think of include :| way better than my answer
0

->Load wont 'execute' the PHP file, it will see the raw source, which probably wont be much use.

You can possibly use the http wrapper, functionality - that actully fetches the page over the web. http://php.net/manual/en/wrappers.http.php

Because it's via the web, the php is executed by the web-server as normal.

 $doc->Load('http://your-domain.com/path/to/file.php');

But not very efficient.

(there are other ways to execute the file - perhaps using shell_exec. Or can just 'include' the file, after wrapping it in output buffering. But the above is the quickest way)

Comments

0

what you could do is edit the complex file adding:

ob_start() 

at the top and

$contents = ob_get_clean();

at the end

Then something like:

if(isset($_GET['getLimitedContent'])){
   $doc = new \DomDocument;
   $doc->loadHTML($contents);
   // parse out and echo the required elements
}else{
   echo $contents;
}

Comments

0

This should work, I think.

// init the CURL
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0); // we do not want to process the headers later
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // return response as a string
curl_setopt($ch, CURLOPT_URL, 'www.mywebsite.com/file.php'); // the URL

// execute the curl
// this will basically call the URL and your PHP will execute as intended
$result = curl_exec($ch);

// use loadHTML() here
$doc->loadHTML($result);

Comments

0

You can use $doc->loadHTMLFile('file.php'); if your file.php generate in output some HTML and use $elems = $doc->getElementById('content');

and do a foreach on the nodes ;)

somes exemples here : http://php.net/manual/fr/domdocument.loadhtmlfile.php

Edit : See test.php call test2.php

test.php

<?php
$doc = new DomDocument;
$doc->loadHTMLFile('test2.php');
$element = $doc->getElementById('tested')->textContent;
print_r($element);
?>

test2.php

<?php
echo"
<html>
<head></head>
<body>
<div id='tested'>texte ok</div>
</body>
</html>";
?>

and its work :)

1 Comment

This will actually executed the PHP file and generate the HTML?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.