that function DOMNode::getNodePath works for any DOMNode, you just have to locate the node for which you want the path.
You could use DOMDocument::getElementById or DOMDocument::getElementsByTagName to locate all the <p> nodes and then check their contents for the text string you are after. Once you find some DOMNode that contains the text you want, you can output getNodePath for it.
$html = trim(file_get_contents("foo.html"));
$search_string = "YES";
$dom = new DOMDocument("1.0", "utf-8");
$dom->loadHTML($html)
or die("could not load html");;
$p_tags = $dom->getElementsByTagName("p");
foreach ($p_tags as $p_tag) {
if (preg_match("/" . preg_quote($search_string) . "/", $p_tag->nodeValue)) {
echo "found search string in " . $p_tag->getNodePath() . PHP_EOL;
}
}