Search code examples
symfonydomcrawler

How to Get text from element, excluding some other elements inside that


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">&nbsp;</span></div></dd></dl>12 23 45 88 99</li>

Solution

  • You can get element html and then get rid of the tags

    preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $node->html());