2017-01-16 83 views
0

我已經搜索並測試了幾個小時,準備放棄。 我有一個HTML頁面,現在會改變每一個,然後,它的結構是這樣的....

PHP代碼從html頁面提取數據,包括標籤

100 or so lines of HTML 
<div class="the start of the info I want"> 
500 lines of HTML that I want to extract 
<div class="end of the info I want"> 
more lines of HTML 

這是我的代碼不工作,我所試過的一處。

<?php 
$data = file_get_contents('http://www.soemstupidsite.xyz'); 
$regex = '#<div class="the start of the info I want">(.*?)<div 
class="end of the info I want">#'; 
preg_match($regex,$data,$match); 
print_r($match); 
echo $match[1]; 
?> 

返回以下錯誤:
PHP公告:未定義抵消:1 /home/www/mycrapcode.php第7行

到底什麼是我做錯了什麼?

+0

'的var_dump($比賽)'來看看它返回 –

+1

我假設的偏移誤差是因爲數組爲空是什麼的print_r($比賽);顯示。 – DeathRox

+0

'回波$匹配[1];'這行拋*備註*,這是因爲陣列'$ match'是空的。 –

回答

1
$regex = '/<div class="the start of the info I want">(.*?)<div 
class="end of the info I want">/s'; 
+0

哇,一個爛的「缺失。這解決了它。非常感謝。如果你曾經在澳大利亞內陸地區看過我,我欠你一個啤酒隊友! – DeathRox

+0

確定男人,很高興) –

0

請閱讀一下關於正則表達式修飾符/標誌here

你需要的標誌,是s標誌,所以你的選擇將多條線路上工作。

與示例代碼:

<?php 
$data = file_get_contents('http://www.soemstupidsite.xyz'); 
$regex = '#<div class="the start of the info I want">(.*?)<div class="end of the info I want">#s'; 
preg_match($regex,$data,$match); 
print_r($match); 
echo $match[1]; 
?> 

另外:正則表達式必須在1號線,否則將無法正常工作。

+0

正則表達式在粘貼時包在一行上。謝謝你對's'標誌的解釋。 Alexandr Malyita在6分鐘內擊敗你,爲我尋找半天的答案。如果你一直在我的路上,我也會找到你的啤酒! – DeathRox

相關問題