Hello,

I'm trying to use this module to parse a $node->body field as suggested on the project page for this module.

I have tried using the example provided AND also using the following syntax:

$html = str_get_html($node->body);
$ret = $html->find('a');
print_r($ret);

In this example my $node->body is four short paragraphs of valid code and an anchor tag (only one) is embedded the last one. When I attempt to render the page I do not get an array with one anchor tag as expected in the $ret variable. I get a page load that does not stop rendering recursive lists of elements in the page. The first element is the anchor tag, the rest is a never ending loop of the other page elements.

The system stamps the word *RECURSION* on each element.

I cannot figure out what is making this behave so strangely. I am new to this module and simplehtmldom so I'm not sure where to begin. I've been reading the documentation on the Sourceforge website but they do not use a consistent variable naming convention so it is difficult to determine what the right approach is.

Ideally, I just want to end up with an array of the elements I'm requesting, be it paragraphs or anchor tags. The html is very simple (ha!) so it seems like simplehtmldom is probably the right tool for the job. Any suggestions?

Comments

Anonymous’s picture

I was able to make some progress with the example on the project page but it basically involves creating a new array of items that I pull from innertext or outertext. Attempting to render any objects provided by simplehtmldom with print_r() is not advisable!

Thanks for the example on the project page. I'm using that, plus the "Magic attributes" tab on this page: http://simplehtmldom.sourceforge.net/manual.htm

Konstantin Komelin’s picture

Category: Bug report » Support request
Issue summary: View changes
Status: Active » Fixed

In you case $html is object that have cross-references, so please don't use print_r(), use something like this instead:

$html = str_get_html($node->body);
$ret = $html->find('a');
foreach ($html->find('a') as $e) {
  echo $e->outertext;
}

or

$html = str_get_html($node->body);
$ret = $html->find('a');
foreach ($html->find('a') as $e) {
  echo $e;
}

or

$html = str_get_html($node->body);
$ret = $html->find('a');
foreach ($html->find('a') as $e) {
  echo $e->plaintext;
}

depending on your needs.

Let me know if you have any other questions.
Konstantin

Anonymous’s picture

Thanks for the reply! Wow, this question takes me back! I posted it so long ago. :)

I'm still using this module in projects all these years later. It is a great resource! Sometimes I even wish this was in core.

Konstantin Komelin’s picture

Thanks, Ryan. Since you're a constant user of this module I suggest trying 6.x-2.x and 7.x-2.x branches. They are going to be released soon.

The main difference is that simplehtmldom library was extracted from the module package to make possible to use any library versions you like, for example the latest one 1.5.
Now the library should be located in sites/all/libraries/simplehtmldom. See more details in README.txt.

Cheers, Konstantin

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.