sunra / php-simple-html-dom-parser

PHP Simple HTML DOM Parser adaptation for Composer and PSR-0
1.3k stars 352 forks source link

Check if parent tag has id, attribute or class. #45

Closed ahoiroman closed 7 years ago

ahoiroman commented 7 years ago

Hello,

I am looping through a HTML string as follows:

    foreach ( $dom->find( 'text' ) as $element ) {

        if ( !in_array( $element->parent()->tag, $excludedParents ) ) {             
            $element->innertext = preg_replace(
                '/(?<!\w)' . preg_quote( $search, "/" ) . '(?!\w)/i',
                $replace,
                $element->innertext
            );
        }
    }

This works fine for excluded parents like a, div or em, but not for a.test or div#test. Is there an elegant way to solve that?