2010-03-20 54 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); 
} 
}