2010-03-20 24 views
1

我該如何解決這個問題?REGEX(。*)和換行

REGEX: 
//REGEX 
$match_expression = '/Rt..tt<\/td> <td>(.*)<\/td>/'; 
preg_match($match_expression,$text,$matches1); 
$final = $matches1[1];  


//THIS IS WORKING 
<tr> <td class="rowhead vtop">Rtštt</td> <td><img border=0 src="http://somephoto"><br /> <br />INFO INFO INFO</td> 
</tr> 


//THIS IS NOT WORKING 
<tr> <td class="rowhead vtop">Rtštt</td> <td> <br /> 
IFNO<br /> 
INFO<br /></td></tr> 
+0

http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/ 1732454#1732454 – 2010-03-20 17:30:02

回答

5

這正是您不應該使用正則表達式從HTML文檔中提取數據的原因。

標記結構是如此的隨意以至於它太簡單太不可靠了,這就是爲什麼我不會給你一個正確的正則表達式來使用,因爲沒有(其他用戶給出的解決方案可能會工作......直到他們打破)。使用DOM解析器(如DOMDocumentphpQuery)從您的文檔中提取數據。

下面是一個例子使用phpQuery

$pq = phpQuery::newDocumentFile('somefile.html'); 
$rows = $pq->find('td.rowhead.vtop:parent'); 

$matches = array(); 

foreach($rows as $row) { 
    $matches[] = $row->eq(1)->html(); 
} 
0
$s = explode('</tr>',$str); 
foreach($s as $v){ 
$m=strpos($v,"img border"); 
if($m!==FALSE){ 
    print substr($v,$m); 
} 
}