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