1

I'm new to domdocument. I'm trying to fetch plaintext or inner text from the span tag that belongs to a specific class

For example

<span class="test">hello world</span> // Need to retrieve hello world text 

This is what I have done so far

$dom = new domDocument;
@ $dom->loadHTML($url);
$classname="test";
$finder = new DomXPath($dom);
$spaner = $finder->query("//span[contains(@class, '$classname')]"); // this returns  [length] => 2 which means two classes present with the name of "test", I need to fetch the first one

1 Answer 1

1

You are almost there. Assuming that your target <span> is the first in the $dom, try this:

$spaner = $finder->query("//span[contains(@class, '$classname')][1]");
echo $spaner[0]->textContent

; Output should be:

hello world
Sign up to request clarification or add additional context in comments.

Comments

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.