How to get text from an element, except for some other elements inside

I am using domCrawler in a symfony framework. I scanned the content from html using it. Now I need to get the text inside the element with the identifier. I can use text using the following code:

 $nodeValues = $crawler1->filter('#idOfTheElement')->each(function (Crawler $node, $i) { return $node->text(); }); 

The element ( #idOfTheElement ) contains some spaces, buttons, etc. (also having some classes). I do not want the contents inside them. How to get text from an element, with the exception of some other elements inside.

Note. The text I wanted to receive has no other shell except the #idOfTheElement element

The html is as follows:

 <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> 
+5
source share
2 answers

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

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

First remove the child nodes:

 $crawler1->filter('#idOfTheElement')->each(function (Crawler $crawler) { foreach ($crawler as $node) { $node->parentNode->removeChild($node); } }); 

Then get the text without child nodes:

 $cleanContent = $crawler1->filter('#idOfTheElement')->text(); 
0
source

All Articles