html没保存,如何在没有HTML包装的情况下保存DOMDocument的HTML?

如何在没有HTML包装的情况下保存DOMDocument的HTML?

我是下面的函数,我很难输出DOMDocument而没有附加XML,HTML,体体和p内容输出之前的标签包装。建议的解决办法:$postarray['post_content'] = $d->saveXML($d->getElementsByTagName('p')->item(0));

只有当内容中没有块级元素时才能工作。但是,当它这样做时,如下面使用H1元素的示例所示,SaveXML的结果输出被截断为.

If you like

我被指出这篇文章是一种可能的解决办法,但我不明白如何将它实现到这个解决方案中(请参阅下面的注释)。

有什么建议吗?function rseo_decorate_keyword($postarray) {

global $post;

$keyword = "Jasmine Tea"

$content = "If you like 

jasmine tea

 you will really like it with Jasmine Tea flavors.

This is the last ocurrence of the phrase jasmine tea within the content. If there are other instances of the

keyword jasmine tea within the text what happens to jasmine tea."

$d = new DOMDocument();

@$d->loadHTML($content);

$x = new DOMXpath($d);

$count = $x->evaluate("count(//text()[contains(translate(., 'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 'abcdefghjiklmnopqrstuvwxyz'),

'$keyword') and (ancestor::b or ancestor::strong)])");

if ($count > 0) return $postarray;

$nodes = $x->query("//text()[contains(translate(., 'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 'abcdefghjiklmnopqrstuvwxyz'), '$keyword')

and not(ancestor::h1) and not(ancestor::h2) and not(ancestor::h3) and not(ancestor::h4) and not(ancestor::h5) and not(ancestor::h6)

and not(ancestor::b) and not(ancestor::strong)]");

if ($nodes && $nodes->length) {

$node = $nodes->item(0);

// Split just before the keyword

$keynode = $node->splitText(strpos($node->textContent, $keyword));

// Split after the keyword

$node->nextSibling->splitText(strlen($keyword));

// Replace keyword with keyword

$replacement = $d->createElement('strong', $keynode->textContent);

$keynode->parentNode->replaceChild($replacement, $keynode);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值