2014-03-28 89 views
2

如何從div類獲取數據?從div類獲取數據

Example: A <div class=ab>1 &nbsp; 2</div>b <div class=ab>3 &nbsp; 4</div>c. 

我想:1 &nbsp; 23 &nbsp; 4等 - <div class=ab></div>之間。

Second example: http://www.imdb.com/title/tt29747"> 

我想:tt29747 - http://www.imdb.com/title/">之間。

With strstr一切都很好,除了我只得到第一個結果。我試着在這裏建立了一些解決方案,但沒有成功,超越了我的正則表達式謝謝!

回答

1

嘗試使用DOMDocument()而不是正則表達式解析HTML。

然而,這裏是解析假設會有的正則表達式沒有嵌套div

$html= 'Example: Lorem <div class=ab>1 &nbsp; 2</div>ipsum <div class=ab>3 &nbsp; 4</div>dolor.'; 
preg_match_all('|<div class=ab>([^<]*)</div>|i', $html, $m); 
print_r($m[1]); 

而對於解析標題ID:

$html = 'http://www.imdb.com/title/tt29747">'; 
preg_match('|imdb.com/title/(tt\d+)|i', $html, $m); 
print_r($m[1]); 
+0

其在'$ M [1] [0 ]'也許你正在看$ m [1] [1]'! –

+0

你最後的評論說它的''ab「'帶引號,其中代碼中只有'ab'。可能是這個原因嗎?按照我在示例中顯示的那樣運行代碼。看看它是如何返回的。 :-) –

+0

從我上面的代碼:用這個'$ html ='

1   2
';'代替行並運行我的代碼。有用! –