第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

與preg_match_all匹配的 PHP

與preg_match_all匹配的 PHP

PHP
冉冉說 2022-09-30 16:16:28
我的任務(wù)是從HTML中提取數(shù)據(jù),我需要為HTML中的每組p標(biāo)簽獲取數(shù)據(jù)數(shù)組。下面是一個(gè)示例。<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>此 HTML 將重復(fù)多次,使“標(biāo)題”和“1234”標(biāo)簽保持不變,然后在某個(gè)點(diǎn)切換到不同的標(biāo)簽?!绊敳俊焙汀白髠?cè)”值將在整個(gè) HTML 中不斷變化。我有能力循環(huán)訪問現(xiàn)有的“Title”和“1234”標(biāo)簽,以匹配這部分內(nèi)容。$title_label = 'Title';$number_label = '1234';preg_match_all('%\d{2}px; white-space: nowrap;">$title_label </p>%', $html_content, $array_match);$array_cost_name = $array_match[1];$array_return_name = $array_match[2];$array_number_name = $array_match[3];然后,我需要 3 個(gè)數(shù)組來包含最后 3 個(gè)標(biāo)簽字段。對(duì)于提供的示例 HTML,我希望“$30”、“$10,000,000”和“3,000,000”是每個(gè)數(shù)組的第一個(gè)值。我不知道如何編寫正則表達(dá)式來處理這種情況。任何人都可以幫忙嗎?
查看完整描述

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


查看完整回答
反對(duì) 回復(fù) 2022-09-30
?
守候你守候我

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


查看完整回答
反對(duì) 回復(fù) 2022-09-30
?
喵喔喔

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>


查看完整回答
反對(duì) 回復(fù) 2022-09-30
  • 3 回答
  • 0 關(guān)注
  • 123 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)