Search code examples
phpscreen-scrapingsimple-html-dom

PHP simplehtmldom read only viewable text


I have the following html format

 <p>This is viewable <span style="display:none">This is not viewable</span></p>

I want to use php simplehtmldom to extract only the "This is viewable" part.

Is there anyway to do it directly?


Solution

  • Sure you can, just remove that text:

    $str = '<p>This is viewable <span style="display:none">This is not viewable</span></p>';
    
    $html = str_get_html($str);
    
    foreach($html->find('[style*=display:none]') as $el){
      $el->innertext = '';
    }
    
    echo $html->find('p', 0)->text();
    // This is viewable