3 回答

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊
正則表達(dá)式不是執(zhí)行此任務(wù)的正確工具,XML解析器要容易得多:
$html = '<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 63px; white-space: nowrap;">Title </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 349px; white-space: nowrap;">1234 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 461px; white-space: nowrap;">$30 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 563px; white-space: nowrap;">$10,000,000 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 777px; white-space: nowrap;">3,000,000 </p>';
$doc = new DOMDocument();
$doc->loadHTML($html);
$xml = simplexml_import_dom($doc);
$parts = $xml->xpath('//p[@class="ft01"]/text()'); // find all texts inside p tags, with class ft01
$array_cost_name = (string) $parts[2];
$array_return_name = (string) $parts[3];
$array_number_name = (string) $parts[4];
echo $array_cost_name ; // $30
echo $array_return_name ; // $10,000,000
echo $array_number_name ; // 3,000,000

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊
你可以使用一個(gè)簡(jiǎn)單的全局正則表達(dá)式 /ace:不換行;”>(.*) <\/p>/
或沿線的任何內(nèi)容來獲取您要查找的組,然后刪除前 2 個(gè)項(xiàng)目以僅獲取最后 3 個(gè)項(xiàng)目。下面是一個(gè)示例和一個(gè)用于測(cè)試它的鏈接。
$html_content = '<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 63px; white-space: nowrap;">Title </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 349px; white-space: nowrap;">1234 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 461px; white-space: nowrap;">$30 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 563px; white-space: nowrap;">$10,000,000 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 777px; white-space: nowrap;">3,000,000 </p>';
preg_match_all('/ace: nowrap;">(.*) <\/p>/', $html_content, $array_match);
$array_match = array_slice($array_match[0], 2); ;
print_r($array_match);
http://sandbox.onlinephpfunctions.com/code/5ac69d44ff8168b4b21133c46dfa9c6db6986b6a

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊
通過正則表達(dá)式,您可以嘗試以下方式:
\preg_match_all('/<p.*>(.*)<\/p>/', $html, $out); $result = $out[1];
這將捕獲標(biāo)記之間的所有字符。<p></p>
- 3 回答
- 0 關(guān)注
- 123 瀏覽
添加回答
舉報(bào)