简体   繁体   中英

How update text based on HTML tags

I have a very basic example:

 <div><span>Lorem ipsum dolor sit amet, elit</span>consectetur adipiscing</div>

I want to replace word "dolor" to "some_another_word" when it around of tag's <div>...</div> . The "dolor" word can place both inside and outside the div 's

My current code is next:

$html = '<div><span>Lorem ipsum dolor sit amet, elit</span>consectetur adipiscing</div>';

$docs = new \DOMDocument();
$docs->loadHTML( $html );

$els = $docs->getElementsByTagName('*');

foreach ( $els as $node ) {
    if ( 'div' === $node->nodeName ) {
        $node->textContent = str_replace('dolor', 'some_another_word', $node->textContent);
    }
}

var_dump( $docs->saveHTML() );

The result of my code is:

<html><body><div>Lorem ipsum some_another_word sit amet, elit consectetur adipiscing</div></body></html>

I am losing span tag that I need. How can I prevent it?

You can use an XPath expression to target, quite precisely, the content that you wish to manipulate if you craft the query correctly. The following should give an idea how you can apply that idea.

$html = '<div><span style="color:red">Lorem ipsum dolor sit amet, elit</span>consectetur adipiscing</div>';
$word = 'dolor';
$replace = '#### banana ####';

try{

    libxml_use_internal_errors( true );

    $dom=new DOMDocument;
    $dom->preserveWhiteSpace = false;
    $dom->validateOnParse = false;
    $dom->standalone=true;
    $dom->strictErrorChecking=true;
    $dom->substituteEntities=true;
    $dom->recover=true;
    $dom->formatOutput=false;
    $dom->loadHTML( $html );

    $errors = libxml_get_errors();
    libxml_clear_errors();


    if( !empty( $errors ) ) {
        throw new Exception( implode( PHP_EOL, $errors ) );
    }
    $xp=new DOMXPath( $dom );

    /* The XPath expression */
    $query='//div/span[ contains( text(),"'.$word.'") ]';

    $col=$xp->query( $query );
    if( !empty( $col ) ){
        foreach( $col as $index => $node ){
            $node->nodeValue = str_replace( $word, $replace, $node->nodeValue );
        }

        /* output to browser or save to file */
        echo $dom->saveHTML();  

    } else {
        throw new Exception( sprintf( 'Empty nodelist - XPath query %s failed', $query ) );
    }
    $dom=$xp=null;
}catch( Exception $e ){
    printf( 'Caught Exception -> Trace:%s Message:%s Code:%d', $e->getTraceAsString(), $e->getMessage(), $e->getCode() );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM