1

I'm trying to use Simple HTML DOM Parser to extract some html from page B and insert the html code into the current page. (Please be gentle, I'm a PHP newbie)

Page B html I'm targeting looks like this: <div id="testdiv">B</div>

I need the same code to be inserted in current page.

My PHP on current page:

<?php
include_once '%resource(simple_html_dom.php)%';
$ret = Array();
$html = file_get_html('b.html');
$ret[] = $html->find('div[id=testdiv]', 0);
$ret->outertext = $ret->makeup() . $ret->innertext . '</div>';
echo $ret;
?>

I've tried to cobble this together, but still no luck. I would expect when viewing the current page to see "B", but I definitely need all the html coding to be there as well ;)
Currently getting an error "Call to a member function makeup() on a non-object"

P.S. Once that's running, I probably need a way to bring in the css and js that's on pg B.

Thanks for the help, Bill

UPDATED Code:

<?php
include_once '%resource(simple_html_dom.php)%';
$html = file_get_html('b.html');
foreach($html->find('div[id=testdiv]') as $ret)
  echo $ret;
?>

So now I need to be able to get specif css & js files from page B so whatever content is in the div will render properly. Here's an example Current Page If you look in the header of Page B there will be a typical css and js file called b_files/stacks_page_page1.css & b_files/stacks_page_page1.js

These are the files I need to have referenced in the current page w/ the relative path changed as necessary. I also added a Page C which is in a folder (B is in root w/ current page) so path will be differnt. I guess combining the css files together & js files together would be good so there aren't a bunch of dom calls.

Overall, what's the code to bring those files in?

3 Answers 3

1

There are a number of problems with the code. $ret[] means push onto the array, but then the next line you are calling object methods, which is why you get the error.

Once that problem is fixed, $ret->makeup is not a valid method according to the API (unless you have modified/extended it). I'm not sure what you think you're doing with $ret->outertext = $ret->makeup() . $ret->innertext . '</div>', but if you're expecting B in your output:

<?php
include_once 'simple_html_dom.php';
$html = file_get_html('b.html');
$ret = $html->find('div[id=testdiv]', 0);
if ($ret) {
    echo $ret->innertext;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I eliminated outer/inner stuff. See Updated Code & final question.
1
foreach($html->find('div[id=testdiv]') as $content)
    echo $content;

the result woud be <div id="testdiv">B</div> and it will be displayed on the browser as

B

if you want to display the code:

foreach($html->find('div[id=testdiv]') as $content)
    echo "<xmp>".$content."</xmp>";

real result: <xmp><div id="testdiv">B</div></xmp>

then on the browser's display:

<div id="testdiv">B</div>

7 Comments

Thanks user, using the foreach works. See Updated Code above and final css/js question.
no problem! please vote up my answer IF it really helped you. :-) obviously, i'm trying to gain more reputation. :-)
I did vote up your answer :) Still need that final question answered. Thanks, Bill
I've read your css/js question. What do you mean, you want to get the "css"? Can you cite up one example please
Ok, updated the question and gave complete explanation and example. Let me know if you need more info. Bill
|
1

You are getting the "Call to a member function makeup() on a non-object" because $ret is an array and doesnt have a method called "makeup()."

I am not sure exactly all you are trying to do but something like this might get you a little closer. I am assuming you have some function named makeup() that produces a string "< div>":

Not Tested

<?
php include_once '%resource(simple_html_dom.php)%';  
$html = file_get_html('b.html'); 
$ret = $html->find('div[id=testdiv]', 0)->innertext; 
$ret = makeup() . $ret . '</div>'; 

echo $ret; 
?> 

1 Comment

Thanks Colin, I saw makeup on another post - thought it was a standard PHP function - I'm going to eliminate that. See Updated code above and final question.

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.