[英]How update text based on HTML tags
我有一个非常基本的例子:
<div><span>Lorem ipsum dolor sit amet, elit</span>consectetur adipiscing</div>
我想在标签的<div>...</div>
周围将单词“ dolor”替换为“ some_another_word”。 “ dolor”一词可以放在div
的内部和外部
我当前的代码是下一个:
$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() );
我的代码的结果是:
<html><body><div>Lorem ipsum some_another_word sit amet, elit consectetur adipiscing</div></body></html>
我丢失了所需的span
标签。 我该如何预防?
如果正确编写查询,则可以使用XPath
表达式精确定位要处理的内容。 下面应该给出一个想法,您可以如何应用该想法。
$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.