Reputation: 3677
I'm using domCrawler
in symfony framework. I crawled contents from html using it. Now I need to get the text inside an element with ID. I'm able to fecth the text by using the code below:
$nodeValues = $crawler1->filter('#idOfTheElement')->each(function (Crawler $node, $i) {
return $node->text();
});
The element(#idOfTheElement
) contains some spans, buttons etc (those having some classes also). I don't want the contents inside those. How to Get text from element, excluding some other elements inside that.
Note: The text I wanted to fetch, does not have any other wrapper, other than the element #idOfTheElement
The Html is look like below:
<li id='#idOfTheElement'>Tel :<button data-pjtooltip="{dtanchor:'tooltipOpposeMkt'}" class="noMkt JS_PJ" type="button">text :</button><dl><dt><a name="tooltipOpposeMkt"></a></dt><dd><div class="wrapper"><p><strong>Signification des pictogrammes</strong></p><p>Devant un numéro, le picto <img width="11" height="9" alt="" src="something"> signale une opposition aux opérations de marketing direct.</p><span class="arrow"> </span></div></dd></dl>12 23 45 88 99</li>
Upvotes: 6
Views: 2747
Reputation: 1583
First remove child nodes:
$crawler1->filter('#idOfTheElement')->each(function (Crawler $crawler) {
foreach ($crawler as $node) {
$node->parentNode->removeChild($node);
}
});
Then get text without child nodes:
$cleanContent = $crawler1->filter('#idOfTheElement')->text();
Upvotes: 2
Reputation: 1814
You can get element html and then get rid of the tags
preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $node->html());
Upvotes: 3