2013-01-21 52 views
0

現在我有一個XPath的搜索功能,如下所示:PHP XPath查詢與包含和正則表達式

$paragraph = $xmldoc->xpath("//p[contains(., '$wordsearch')]"); 

我想知道是否有可能讓$wordsearch是一個正則表達式,所以我搜索看着像這樣:

$paragraph = $xmldoc->xpath("//p[contains(., '$regularExpression')]"); 

感謝您的幫助。

+0

它[不支持](http://stackoverflow.com/questions/405060/can-i-use-a-regex-in-an-xpath-expression),因爲在PHP中,底層庫[只實現XPath 1.0](http://stackoverflow.com/questions/2085632/will-xpath-2-0-and-or-xslt-2-0-be-implemented-in-php)。 – mario

+0

erg,php讓我失望 – Jeff

+0

你可以使用regex來過濾返回數組。 – hakre

回答

1

你可以用正則表達式,而不是濾鏡陣列:

$paragraph = array_filter(
    $xmldoc->xpath("//p"), 
    function ($p) use ($regularExpression) { 
     return preg_match($regularExpression, $p); 
    } 
); 

array_filter