我是下面的函数,我在努力输出DOMDocument而没有在内容输出之前附加XML,HTML,body和p标签包装器.建议的修复:
$postarray['post_content'] = $d->saveXML($d->getElementsByTagName('p')->item(0));
Run Code Online (Sandbox Code Playgroud)
仅在内容中没有块级元素时才有效.但是,当它执行时,如下面的例子中的h1元素,saveXML的结果输出被截断为...
<p>如果你喜欢</ p>
我已经指出这篇文章可能是一种解决方法,但是我无法理解如何将它实现到这个解决方案中(参见下面的注释).
有什么建议?
function rseo_decorate_keyword($postarray) {
global $post;
$keyword = "Jasmine Tea"
$content = "If you like <h1>jasmine tea</h1> 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 = …Run Code Online (Sandbox Code Playgroud) 我正在使用PHP的DOMDocument来解析和规范化用户提交的HTML,使用该loadHTML方法解析内容,然后通过以下方式获得格式良好的结果saveHTML:
$dom= new DOMDocument();
$dom->loadHTML('<div><p>Hello World');
$well_formed= $dom->saveHTML();
echo($well_formed);
Run Code Online (Sandbox Code Playgroud)
这可以很好地解析片段并添加适当的结束标记.问题是,我也越来越一堆标签,我不想如<!DOCTYPE>,<html>,<head>和<body>.我知道每个格式良好的HTML文档都需要这些标记,但我正在规范化的HTML片段将被插入到现有的有效文档中.