2014-02-23 26 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