本文目錄一覽:
PHP html正則提取div數據
正則提取div數據主要是使用PHP的file_get_content()函數。
具體示例:
HTML代碼:
div class=”chartInfo”
div class=”line”/div
div class=”tideTable”
strong潮汐表/strong數據僅供參考
table width=”500″ border=”0″ cellspacing=”0″ cellpadding=”0″
tbodytr
td width=”100″pspan潮時 (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時區:-1000 (東10區) 潮高基準面:在平均海平面下174CM/h2
/div
div class=”chart”
/div
/div
首頁先用file_get_content或curl獲取內容部分
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正則提取 DIV CLASS 數據
用PHP正則表達式匹配,就可以提取class裡面的數據,並將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]);
?
運行結果
如何用PHP 取DIV標籤內容
如果你要和之間的所有源碼,用preg_match就可以,不用preg_match_all,如果你要裡面的所有的標籤中的內容,可以用preg_match_all//提取所有代碼$pattern=’/(.+?)/is’;preg_match($pattern,$string,$match);//$match[0]即為和之間的所有源碼echo$match[0];//然後再提取之間的內容$pattern=’/(.+?)/is’;preg_match_all($pattern,$match[0],$results);$new_arr=array_unique($results[0]);foreach($new_arras$kkk){echo$kkk;}
php正則 提取DIV裡面的數據
你這裡內容是固定的日期時間格式,直接寫個相應的格式就行了啊,其他不用管
(\d{4}-\d{2}-\d{2})
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/227809.html