spykr
spykr

Reputation: 67

Getting the text between two spans with "Simple HTML DOM"

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?

Upvotes: 3

Views: 5575

Answers (3)

Manwal
Manwal

Reputation: 23816

find('text', $index)

This will give you blocks of text.

Try this:

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

output:

TEXT I NEED TO GET

DEMO

Upvotes: 1

Gordon
Gordon

Reputation: 316969

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

Upvotes: 4

Yoshi
Yoshi

Reputation: 54649

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 '
}

Upvotes: 0

Related Questions