2013-03-24 34 views
0

我想從裏面TD網頁過濾數據,它是這樣的:的preg_match對TD

<td colspan="2">several anchor,bold and other html tags are inside this td</td> 

我已經使用這個的preg_match但所有其他的TD它給人的輸出,但在上述情況下,沒有給出任何輸出。

preg_match("/\<td colspan\=\"2\"\>(.*)\<\/td\>/",$str,$title); 

這裏充滿TD:

<td colspan="2"> 
     <div align="left" style="width:370; height:315;"> 
      <ins style="display:inline-table;border:none;height:280px;margin:0;padding:0;position:relative;visibility:visible;width:336px">  

      <ins style="display:block;border:none;height:280px;margin:0;padding:0;position:relative;visibility:visible;width:336px" id="aswift_1_anchor"><iframe width="336" scrolling="no" height="280" frameborder="0" style="left:0;position:absolute;top:0;" name="aswift_1" id="aswift_1" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){w.location.replace(h)}}" allowtransparency="true" hspace="0" vspace="0" marginheight="0" marginwidth="0"></iframe></ins></ins> 
      </div><p> When starting out sometimes it is a good idea to write down your   <a href="#" style="text-decoration: underline !important;position:static;font-family:inherit !important;font-weight:inherit !important;font-size:inherit !important;" class="kLink" id="KonaLink1"> 
     <font color="blue" style="color: blue !important; font-family:inherit !important;font-weight:inherit !important;font-size:inherit !important;position:static;">     <span style="color: blue !impor If you seriously want to take back control of your money you need to build a <a href="http://ezinearticles.com/?To-Set-Up-a-Personal-Budget-Get-a-Pencil-and-Paper&amp;id=1629478">Personal Budget</a>. To learn more about creating a budget please visit the website <a href="http://household-budget.home-choices-net.com">Household Budgets by clicking here</a>. </p><p> </p><p><!-- google_ad_section_end --> 

       </p><p> 
     <font style="color:02679D; font-size:12"><b><font color="000000">Related Articles - 

     </font> 
      </b></font> 
     </p><p><table width="100%" border="0"><tbody><tr> 
     <td align="center"> 
      <br><br><br><br> 

     <br><br> 

      </font></p></td></tr></tbody></table> 
      </p></td> 
+0

你知道使用正則表達式來解析HTML是錯誤的嗎? (那麼,至少有爭議(除非你真的知道你在做什麼)) – JackTheRandom 2013-03-24 10:55:24

回答

1

一般不使用正則表達式解析HTML。然而,你的問題是你的正則表達式已經準備好並捕獲所有可能的數據。嘗試添加一個問號:

preg_match("/\<td colspan\=\"2\"\>(.*?)\<\/td\>/",$str,$title); 

問號使得組不成立,並且字符串將在下一個可能的標記上結束。

+0

沒有工作嘗試過。 – tumbinkya 2013-03-24 11:29:09

+0

嘗試([^ <] *?)代替(。*?) – rekire 2013-03-24 13:11:02

+0

我試圖匹配該 SI已經使用這一點,但不工作\t \t的preg_match(「/ \ <\/td\><\/tr\>(。*)\ <\/td\> /「,$ str,$ title); – tumbinkya 2013-03-24 13:31:09

0

您需要添加改性劑:

 preg_match("/\<td colspan\=\"2\"\>(.*)\<\/td\>/s",$str,$title); 

http://php.net/manual/en/reference.pcre.pattern.modifiers.php

s (PCRE_DOTALL) 

如果設定了此修正,在模式中的圓點元字符的所有字符,包括換行符相匹配。沒有它,換行符是 排除。這個修飾符相當於Perl的/ s修飾符。 A 否定類如[^ a]總是匹配換行符 ,與此修飾符的設置無關。

+0

嘗試過,但無法正常工作 – tumbinkya 2013-03-24 11:31:45