preg_match - 查找包含单词 A 而不是 B 的表单


preg_match - find forms that contain word A and not B

我正在使用PHP preg_match来查找和解析页面上的特定表单。到目前为止,我的代码运行良好,但我发现一些不需要的表单现在包含我实际需要的单词,因此这些不需要的形式也被解析。简而言之,我需要解析所有包含单词"findme"的表单,而不是在URL中包含单词"ignoreme"。

这是我的preg_match:

<form action=("|''?)([0-9a-zA-Z:'/'._~'-'?=]+)findme(.*?)'/form>

不幸的是,如果表单URL像/some_url/ignoreme/findme/whatever,代码仍然解析它,这是我不想要的。我应该如何修改代码?

$dom = new DOMDocument;
$dom->loadHTML($yourHTML);
$xpath = new DOMXPath($dom);
$formNodeList = $xpath->query('//form[contains(@action, "findme") and not(contains(@action, "ignoreme"))]');
foreach($formNodeList as $formNode) {
    // do what you want with the DOMNode (see php manual)
}