正則提取div數(shù)據(jù)主要是使用PHP的file_get_content()函數(shù)。
成都創(chuàng)新互聯(lián)公司專(zhuān)注于新華企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開(kāi)發(fā),商城網(wǎng)站制作。新華網(wǎng)站建設(shè)公司,為新華等地區(qū)提供建站服務(wù)。全流程定制開(kāi)發(fā),專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)
具體示例:
HTML代碼:
div?class="chartInfo"??
div?class="line"/div
div?class="tideTable"??
strong潮汐表/strong數(shù)據(jù)僅供參考
table?width="500"?border="0"?cellspacing="0"?cellpadding="0"?
tbodytr??
td?width="100"pspan潮時(shí)?(Hrs)/span/p/td??
td?width="100"p00:58/p/td
td?width="100"p05:20/p/td
td?width="100"p13:28/p/td
td?width="100"p21:15/p/td
/tr??
tr??
tdpspan潮高?(cm)/span/p/td??
td?width="100"p161/p/td??
td?width="100"p75/p/td??
td?width="100"p288/p/td??
td?width="100"p127/p/td??
/tr??
/tbody/table??
h2時(shí)區(qū):-1000?(東10區(qū))??潮高基準(zhǔn)面:在平均海平面下174CM/h2??
/div??
div?class="chart"??
/div??
/div
首頁(yè)先用file_get_content或curl獲取內(nèi)容部分
PHP的正則處理程序:
?php
$ch?=?curl_init();??
curl_setopt($ch,?CURLOPT_URL,?$url);??
curl_setopt($ch,?CURLOPT_POST,?1);??
curl_setopt(?$ch,?CURLOPT_HEADER,?0?);??
curl_setopt(?$ch,?CURLOPT_RETURNTRANSFER,?1?);??
curl_setopt(?$ch,?CURLOPT_POSTFIELDS,?$data?);??
$return?=?curl_exec(?$ch?);??
curl_close(?$ch?);??
$regex4="/div?class=\"tideTable\".*?.*?\/div/ism";??
if(preg_match_all($regex4,?$return,?$matches)){??
print_r($matches);??
}else{??
echo?'0';??
}
?
用PHP正則表達(dá)式匹配,就可以提取class里面的數(shù)據(jù),并將no去除,完整的PHP程序如下
?php
$str='div?class="lot-nums"span?class="no7"/spanspan?class="no8"/spanspan?class="no5"/spanspan?class="no9"/spanspan?class="no2"/spanspan?class="no4"/spanspan?class="no6"/spanspan?class="no0"/spanspan?class="no3"/span?span?class="no1"/span/div';?
$regex='/span?class="no(\d+)"/i';?
preg_match_all($regex,$str,$result);?
print_r($result[1]);?
?
運(yùn)行結(jié)果
可用如下的代碼來(lái)實(shí)現(xiàn):
?php
$str1="|1234|#2354#@2314@
|1314|#2154#@2214@
|1234|#2354#@2314@
|1314|#2154#@2214@";
if(preg_match_all("/\|(\d{4})\|\#(\d{4})\#\@(\d{4})\@/m",$str1,?$out,?PREG_PATTERN_ORDER))
for($i=0;$icount($out[0]);$i++)
{
echo?"{'".$out[1][$i]."','".$out[2][$i]."','".$out[2][$i]."'}br/";
}
?
效果如下所示:
{'1234','2354','2354'}
{'1314','2154','2154'}
{'1234','2354','2354'}
{'1314','2154','2154'}