2014-02-23 36 views
0

我試圖篩選使用的preg_match這似乎是正確的,在查看源代碼相匹配的數據,但是當我運行的代碼,它沒有給對手發現的preg_match輸出

這裏是我的模式

/\<p\>(.*)\<center\>/ 

字符串

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <title>Untitled Document</title> 
    </head> 

    <body> 
    <small>Photo courtesy of <a href="http://www.sxc.hu/photo/899943">hortongrou</a>   </small> 
    </div> 
    <p>I have developed theasional thriller.<br /> 
    <code> </code><br /> 


     <li><strong><a href="">Beginning Ubuntu Linux</a><img style="border:none !important; margin:0px !important;" src="" border="0" alt="" width="1" height="1" />, by Keir Thomas and Jaime Sicam.</strong> For those who like to transition from Windows to Linux, install Ubuntu Linux on one of yoto you.</li> 
    </ol> 
    <p class="alert">For more Linux books that I recommend, browse <a rel="nofollow" href="http://astore.amazon.com/thgest-20">The Geek Stuff bookstore at Amazon.</a></p> 
    <p>There are several hundreds of books on Linux. <strong>What is your favorite Linux book that is not on this list?</strong><br /> 
    <code> </code><br /> 
    <em>If you like this article, please bookmark it on <strong>delicious and Stumble it</strong>.</em></p> 
     <center> 
      <div style="margin-left:2px; margin-top:10px; margin-bottom:10px; "> 

      <!-- AD BLOCK --> 



     </body> 

+0

您的預期產量是多少? –

+2

http://stackoverflow.com/a/1732454/784331 – Bugs

+2

你有沒有考慮過使用HTML解析器? – TimWolla

回答

0

默認情況下.不匹配新的生產線。嘗試添加DOTALLsmodifier像這樣/<p>(.*)<center>/s

+0

請參閱http://www.phpliveregex.com/p/3TW –

0

您正在使用貪婪版本的正則表達式,基本上告訴它重複檢查任何東西,儘可能長,而不是直到它到達<center>。您可以使用?防止這種情況:

/\<p\>(.*?)\<center\>/ 

此外,><不需要被轉義:

/<p>(.*?)<center>/ 
+0

不工作檢查相同的輸出即將到來。你可以在這裏測試http://www.phpliveregex.com/p/3TQ – sachin

+1

@sachin你在測試中沒有任何中心標籤。此外,表單中已經包含了尾隨斜線,因此它們不會與它們一起工作。 –

+0

這不是我的答案,Jan是正確的關於斜槓和中心。 – Anonymous