簡體   English   中英

php preg_match模式問題,正則表達式模式

[英]php preg_match pattern problem,regular expression pattern

<tr  id='ieconn3' >
  <td><table width='100%'><tr><td valign='top'><table width='100%'><tr><td>aaaaa
<br>&nbsp;</td></tr><tr><td> 

我想將aaaaa部分aaaaa<br></td> 我嘗試了很多模式,但是沒有用。 有什么幫助嗎?

您不應該嘗試使用正則表達式來解析HTML,因為HTML不是正則語言,因此無法用正則表達式來描述。 改用正確的HTML解析器。

如果使用的是XHTML,則可以使用SimpleXML將其解析為XML並使用SimpleXMLElement :: xpath進行查詢。 對於HTML文檔,您可以使用Simple HTML DOM Parser DOMDocument甚至可以處理XHTML和HTML。

正如Gumbo指出的那樣,如果您堅持為此使用正則表達式,只會導致巨大的混亂。 但是,如果您確定HTML不會出現這種情況,那么可以做到這一點:

/<tr><td>(.*)<\\/td><\\/tr>/

像這樣使用:

$string = "<tr  id='ieconn3' >
<td><table width='100%'><tr><td valign='top'><table width='100%'><tr><td>aaaaa<br>&nbsp;</td></tr><tr><td>";

$matches = array();
preg_match("/<tr><td>(.*)<\\/td><\\/tr>/", $string, $matches);

print($matches[1]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM