当我运行下面的decorate_keyword()函数时,我遇到了两个我正在尝试解决的错误。第一个似乎最容易解决。我得到......
警告:DOMXPath :: query() [domxpath.query]:表达式无效 在 C:\ Xampplite文件\ htdocs中\测试网站\可湿性粉剂内容\插件\为myplugin \的index.php 第48行
它指向函数中的$ nodes行,并且当它包含空格时显然源于$ keyword变量。 $ keyword =“test”不会抛出错误。我需要更改什么才能克服此错误?
function rseo_decorate_keyword($postarray) {
$keyword = "test";
$content = $postarray['post_content'];
/*
even though I can echo $content and get a string,
I'm getting error: Empty string supplied in loadHTML() when I use this.
so, I have to explicity set it to a string as below to test.
*/
$content = "this is a test phrase";
$d = new DOMDocument();
$d->loadHTML($content);
$x = new DOMXpath($d);
$nodes = $x->query("//text()[contains(.,$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)]");
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 <b>keyword</b>
$replacement = $d->createElement('b', $keynode->textContent);
$keynode->parentNode->replaceChild($replacement, $keynode);
}
$postarray['post_content'] = $d;
return $postarray;
}
答案 0 :(得分:0)
如果contains
XPath函数包含各种字符,例如;&,
,则它应该在引号中包含第二个参数。空间就是其中之一。
$nodes = $x->query("//text()[contains(.,'$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)]");
对于mysql_real_escape_string
错误,由于某种原因,正在传递对象而不是字符串。我不熟悉Wordpress内部:你应该用适当的标签提出一个单独的问题。