DOMXPath :: query():表达式无效

时间:2017-06-16 13:24:15

标签: xpath domxpath

我在类中有这个代码:

...
$document = new DOMDocument( '1.0', 'utf-8');
$document->preserveWhiteSpace = false;
$content = mb_convert_encoding($content, 'HTML-ENTITIES', 'utf-8'); //this fix problems with cyrilic text
if ( !@$document->loadHtml( $content ) )
    return false;
$xpath = new DOMXpath( $document );
$node_list = $xpath->query( $this->target->query_name() ); // getting warning here
...

query_name方法的代码(位于其他类和文件中)是:

public function query_name( $if_arg_needed = "" ){
    return "//*/*[@class=\'product_name\']";
}

包含我的类的两个文件都在utf-8中。 为什么我收到警告:     警告:DOMXPath :: query():...中的表达式无效

此代码可以正常工作:

...
$xpath = new DOMXpath( $document );
$tempStr = "//*/*[@class=\'product_name\']";
$node_list = $xpath->query( $tempStr );

1 个答案:

答案 0 :(得分:1)

您应该像这样设置xpath:

$tempStr = "//*[@class='product_name']";

$tempStr = '//*[@class="product_name"]';