我有以下代碼正則表達式和XPath查詢
<?php
$doc = new DOMDocument;
$doc->loadhtml('<html>
<head>
<title>bar , this is an example</title>
</head>
<body>
<h1>latest news</h1>
foo <strong>bar</strong>
<i>foobar</i>
</body>
</html>');
$xpath = new DOMXPath($doc);
foreach($xpath->query('//*[contains(child::text(),"bar")]') as $e) {
echo $e->tagName, "\n";
}
打印
title
strong
i
這個代碼查找包含單詞「酒吧」的任何HTML元素和它匹配的是有「巴」字像「foobar」我想改變查詢,只匹配單詞「酒吧」沒有任何前綴或後綴
我認爲這可以通過更改查詢搜索每個「酒吧」沒有拿到書後,或前或後或之前
感謝
參考:[使用正則表達式在xpath中使用php過濾屬性](http://stackoverflow.com/q/6823032/367456)(2011年7月), – hakre 2015-08-17 05:32:44