一个正则表达式,如接受的答案基础的解决方案是不来提取HTML文档信息的正确途径。
使用DOMDocument基础的解决方案是这样,而不是:
$str = '
Refference ID |
---|
...
Slot130717919020ffqClE0nRaspoB...
20130717091902413';// Create a document out of the string. Initialize XPath
$doc = new DOMDocument();
$doc->loadHTML($str);
$selector = new DOMXPath($doc);
// Query the values in a stable and easy to maintain way using XPath
$refResult = $selector->query('//table[@class="table2"]/tr[2]/td[1]');
$slotResult = $selector->query('//table[@class="table2"]/tr[2]/td[6]');
// Check if the data was found
if($refResult->length !== 1 || $slotResult->length !== 1) {
die("Data is corrupted");
}
// XPath->query always returns a node set, even if
// this contains only a single value.
$refId = $refResult->item(0)->nodeValue;
$slot = $slotResult->item(0)->nodeValue;
echo "RefId: $refId, Slot: $slot", PHP_EOL;