日期:2014-05-17  浏览次数:20540 次

正则问题,急用,大家帮帮忙啊
<table   width= "96% "   border= "0 "   align= "center "   cellpadding= "0 "   cellspacing= "0 "   id= "[#TABID_2#] "   class= "linetable1 ">
<tr   style= "background:#E3E3E3 "   align= "center ">
<td>
<strong>   用户编码 </strong>  
</td>
<td>
<strong> 开启SMS功能 </strong>  
</td>
<td>
<strong> IP地址   </strong>  
</td>
<td>
<strong> 登陆方式 </strong>    
</td>
</tr>
<tr>
<td>
11111
</td>
</tr>
<tr   align= "center "   id=loop>
<td>
  [#SUB_2#用户编码]  
</td>
<td>
[#SUB_2#开启SMS功能]    
</td>
<td>
  [#SUB_2#IP地址]  
</td>
<td>
[#SUB_2#登录方式]    
</td>
</tr>
<tr>
<td>
2222222
</td>
</tr>
</table>
---------------在以上的HTML中匹配出--------------------------------

<tr   align= "center "   id= "loop ">
<td>
  [#SUB_2#用户编码]  
</td>
<td>
[#SUB_2#开启SMS功能]    
</td>
<td>
  [#SUB_2#IP地址]  
</td>
<td>
[#SUB_2#登录方式]    
</td>
</tr>

------------------注意这里
在 <tr   align= "center "   id= "loop "> 中,id= "loop "的左右可能有其他的标签,也可能没有,
如: <tr   id= "loop ">
<tr   align= "center "   id= "loop ">
<tr   align= "center "   id= "loop "   height= "20 ">
反正就是不关怎么变都要能取出
以上那一段HTML

大家帮帮忙啊。谢谢了

------解决方案--------------------
<?php
$html = < < <EOD
<table width= "96% " border= "0 " align= "center " cellpadding= "0 " cellspacing= "0 " id= "[#TABID_2#] " class= "linetable1 ">
<tr style= "background:#E3E3E3 " align= "center ">
<td>
<strong> 用户编码 </strong>
</td>
<td>
<strong> 开启SMS功能 </strong>
</td>
<td>
<strong> IP地址 </strong>
</td>
<td>
<strong> 登陆方式 </strong>
</td>
</tr>
<tr>
<td>
11111
</td>
</tr>
<tr align= "center " id=loop>
<td>
[#SUB_2#用户编码]
</td>
<td>
[#SUB_2#开启SMS功能]
</td>
<td>
[#SUB_2#IP地址]
</td>
<td>
[#SUB_2#登录方式]
</td>
</tr>
<tr>
<td>
2222222
</td>
</tr>
</table>
EOD;

$pattern = '/( <tr [^ <]*id= "?loop "?.*> .* <\/tr> )/Us ';
if (preg_match($pattern, $html, $match))
{
echo htmlspecialchars($match[1]);
}
else
echo 'no matc