简体   繁体   English

如何基于HTML标签更新文本

[英]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> . 我想在标签的<div>...</div>周围将单词“ dolor”替换为“ some_another_word”。 The "dolor" word can place both inside and outside the div 's “ dolor”一词可以放在div的内部和外部

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. 我丢失了所需的span标签。 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. 如果正确编写查询,则可以使用XPath表达式精确定位要处理的内容。 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() );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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