2011-04-16 26 views
3

提取圖像路徑我有一個樣式表是這樣的:PHP - 從CSS

.d2 { 
    position:absolute; 
    background:url(../img/delete.png) no-repeat 0px 1px; 
    color:#0066CC; 
} 
.reply { 
    position:absolute; 
    background:url(../img/pen.png) no-repeat 0px 1px; 
    top:5px 
} 
#about { 
    position:absolute; 
    background:url(../img/abc.png); 
    top:5px 
} 

我想有沒有重複屬性的圖像路徑。期待的結果fowllow:

array('../img/pen.png', '../img/delete.png') 

回答

2

此測試的代碼將做到這一點:

$imgs = array(); 
$re = '/url\(\s*[\'"]?(\S*\.(?:jpe?g|gif|png))[\'"]?\s*\)[^;}]*?no-repeat/i'; 
if (preg_match_all($re, $text, $matches)) { 
    $imgs = $matches[1]; 
} 
0

如果你正在尋找只爲背景(和他們遵循的選項(URL,重複,休息)這個順序):

preg_match_all('~background:\s*url\(['"]?([^)]+)['"]?\)\s+no-repeat[^;]*;~i', $css, $reg); 

print_r($reg[1]); 

輸出:

Array 
(
    [0] => ../img/delete.png 
    [1] => ../img/pen.png 
) 
+0

對不起該代碼不會驗證。 – 2011-04-17 05:02:33

1

我會逐行讀取文件。

<?php 

$image=array(); 
// Get a file into an array. In this example we'll go through HTTP to get 
// the HTML source of a URL. 
$lines = file('http://www.example.com/'); 

// Loop through our array, show HTML source as HTML source; and line numbers too. 
foreach ($lines as $line_num => $line) { 

if (strpos($line,'no-repeat') 

{ 如果(strpos($線 'PNG'))

{ 
    $urlpos=strpos($line,'url)); 
    $rightone=strpos($line,'(',$urlpos); 
    $leftone=strpos($line,')',$rightone); 
    array_push($images,substr($line,$rightone,$leftone-$rightone); 
    } 
} 

}