2011-04-10 149 views
1

我只想在html文檔中查找帶有preg_match_all的所有元素。讀取文件後,我使用了以下內容:PHP使用preg_match_all找到所有具有style屬性的元素

preg_match_all('<.*style=?.*>',$file,$patterns); 
print_r($patterns[0]); die; 

給出了所有的元素,但與間距和其他的東西了<和>之前。輸出結果中也有一個結束標記(例如:')。我玩弄了preg的表情,但令我瘋狂。有人可以告訴我什麼是正確的語法使用?現在

輸出是:

Array 
(
    [0] => <table style="position:absolute;width:100%;height:100%;"> 
    [1] => <div class="_barcode_pdf417" style="margin:0 auto;width:176px;height:132px;background:#FFF;color:#000;"><div style="margin:0 auto;margin:0;padding:0;border:0"> 
    [2] =>  <div style="position:absolute;width:14px;height:128px;background:#000;"></div> 
    [3] =>  <div style="position:absolute;margin-left:18px;width:2px;height:128px;background:#000;"></div> 
    [4] =>  <div style="position:absolute;margin-left:22px;width:2px;height:128px;background:#000;"></div> 
    [5] =>  <div style="position:absolute;margin-left:26px;width:2px;height:128px;background:#000;"></div> 
........ 
........ 
........ 

但我想:

Array 
(
    [0] => <table style="position:absolute;width:100%;height:100%;"> 
    [1] => <div class="_barcode_pdf417" style="margin:0 auto;width:176px;height:132px;background:#FFF;color:#000;"> 
<div style="margin:0 auto;margin:0;padding:0;border:0"> 
    [2] => <div style="position:absolute;width:14px;height:128px;background:#000;"> 
    [3] => <div style="position:absolute;margin-left:18px;width:2px;height:128px;background:#000;"> 
    [4] => <div style="position:absolute;margin-left:22px;width:2px;height:128px;background:#000;"> 
    [5] => <div style="position:absolute;margin-left:26px;width:2px;height:128px;background:#000;"> 
...... 
...... 

謝謝您的回答!親切的問候。

+1

請參閱[鏈接](http://stackoverflow.com/questions/1732348/regex-比賽開標籤 - 除了-XHTML-自足標籤/ 1732454#1732454)。 – Czechnology 2011-04-10 21:11:28

+0

'<' and '>'被視爲[模式分隔符](http://php.net/regexp.reference.delimiters),而不是字面尖括號匹配。 – salathe 2011-04-10 21:18:49

+0

[(PHP5)可能的重複使用PHP DOM或Regex從HTML中提取標題標籤和RSS提要地址](http://stackoverflow.com/questions/3054347/php5-extracting-a-title-tag-and-rss -feed-address-from-html-using-php-dom-or-reg) – Gordon 2011-04-10 21:31:49

回答

0

我強烈建議不要以這種方式使用正則表達式在(X)HTML上操作,因爲PHP以DOMDocument擴展的形式爲作業提供更高級別的API。您可以使用它來遍歷有效的DOm結構並查找具有特定屬性的元素。操作與Javascript DOM操作非常類似,可以使用GetElementById,GetElementByClassName等類似功能。

您可以使用它迭代體內的子元素(及其子元素)以查找具有已定義樣式的元素。

1
$html = <<< EOF 
[0] => <table style="position:absolute;width:100%;height:100%;"> 
[1] => <div class="_barcode_pdf417" style="margin:0 auto;width:176px;height:132px;background:#FFF;color:#000;"><div style="margin:0 auto;margin:0;padding:0;border:0"> 
[2] =>  <div style="position:absolute;width:14px;height:128px;background:#000;"></div> 
[3] =>  <div style="position:absolute;margin-left:18px;width:2px;height:128px;background:#000;"></div> 
[4] =>  <div style="position:absolute;margin-left:22px;width:2px;height:128px;background:#000;"></div> 
[5] =>  <div style="position:absolute;margin-left:26px;width:2px;height:128px;background:#000;"></div> 
........ 
........ 
........" 
EOF; 


preg_match_all('/([<div|<table]+.*?style.*?>)/i', $html, $result, PREG_PATTERN_ORDER); 
for ($i = 0; $i < count($result[0]); $i++) { 
echo $result[1][$i]; 
} 

將輸出:

<table style="position:absolute;width:100%;height:100%;"> 
<div class="_barcode_pdf417" style="margin:0 auto;width:176px;height:132px;background:#FFF;color:#000;"> 
<div style="margin:0 auto;margin:0;padding:0;border:0"> 
<div style="position:absolute;width:14px;height:128px;background:#000;"> 
<div style="position:absolute;margin-left:18px;width:2px;height:128px;background:#000;"> 
<div style="position:absolute;margin-left:22px;width:2px;height:128px;background:#000;"> 
<div style="position:absolute;margin-left:26px;width:2px;height:128px;background:#000;"> 

雖然,最好的選擇是使用html dom parser

+0

謝謝!我會嘗試。 – Codebeat 2011-08-09 00:06:03

相關問題