3

Basically, I need to get the text between two span tags, and I've tried a bunch of different methods with no solution. I'm using Simple HTML DOM Parser (http://simplehtmldom.sourceforge.net/) too, so what I can do is a little restricted to. Here is the basic setup:

<span class=1>text here</span> TEXT I NEED TO GET <span class=2>more text</span>

Any help?

spykr
  • 67
  • 1
  • 8

3 Answers3

4

The text between the span elements should be a DOMTextNode and sibling to the span elements. If SimpleHTMLDom follows DOM specs you should be able to get it with:

$text = $html->find('span[class=1]', 0)->next_sibling();

If that doesnt work, consider using a more proper parser that is based on libxml, e.g. see

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
1

find('text', $index)

This will give you blocks of text.

Try this:

echo $html->find('text',1);

output:

TEXT I NEED TO GET

DEMO

Manwal
  • 23,450
  • 12
  • 63
  • 93
0

Try PHP Dom:

$dom = new DomDocument;
$dom->loadHtml('
    <span class=1>text here</span> TEXT I NEED TO GET <span class=2>more text</span>
');

$xpath = new DomXpath($dom);
foreach ($xpath->query('//body/text()') as $textNode) {
    echo $textNode->nodeValue; // will be: ' TEXT I NEED TO GET '
}
Yoshi
  • 54,081
  • 14
  • 89
  • 103
  • You might want to add some explanation to that because the XPath is non-obvious when you dont know what loadHTML does to HTML fragments. – Gordon Aug 02 '11 at 12:05
  • @Gordon You're probably right, but it seems this topic is closed. ;) – Yoshi Aug 02 '11 at 13:24