似乎您想使用DOMDocument方法来解析HTML 。如果是这种情况,您可以实例化一个新对象,可能类似于:
$dir = '
get this text
$dom = new DOMDocument();
$dom->loadHTML($dir);
$xpath = new DOMXpath($dom);
$res = $xpath->document->documentElement->textContent;
$textNodes = explode(PHP_EOL, $res);
foreach ($textNodes as $key => $text) {
if ($text == 'mydata') {
echo $textNodes[$key + 1];
break;
}
}
然后,您可以完成您希望对文档执行的所有操作。
输出:
get this text
文档
如果您拥有var_dump($xpath->document->documentElement);,则可以了解如何使用documentElement对象的属性:
twoobject(DOMElement)#3 (18) {
["tagName"]=>
string(4) "html"
["schemaTypeInfo"]=>
NULL
["nodeName"]=>
string(4) "html"
["nodeValue"]=>
string(20) "one
mydata
two
three"
["nodeType"]=>
int(1)
["parentNode"]=>
string(22) "(object value omitted)"
["childNodes"]=>
string(22) "(object value omitted)"
["firstChild"]=>
string(22) "(object value omitted)"
["lastChild"]=>
string(22) "(object value omitted)"
["previousSibling"]=>
string(22) "(object value omitted)"
["nextSibling"]=>
NULL
["attributes"]=>
string(22) "(object value omitted)"
["ownerDocument"]=>
string(22) "(object value omitted)"
["namespaceURI"]=>
NULL
["prefix"]=>
string(0) ""
["localName"]=>
string(4) "html"
["baseURI"]=>
NULL
["textContent"]=>
string(20) "one
mydata
two
three"
}