I have a style sheet like this:
.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
}
I want to get the image paths that have no-repeat attribute. Expecting result as fowllow:
array('..开发者_如何学Python/img/pen.png', '../img/delete.png')
This tested code will do it:
$imgs = array();
$re = '/url\(\s*[\'"]?(\S*\.(?:jpe?g|gif|png))[\'"]?\s*\)[^;}]*?no-repeat/i';
if (preg_match_all($re, $text, $matches)) {
$imgs = $matches[1];
}
I would read the file line by line.
<?php
$image = [];
$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') {
if (strpos($line, '.png')) {
$urlpos = strpos($line, 'url');
$rightone = strpos($line, '(', $urlpos);
$leftone = strpos($line, ')', $rightone);
array_push($images, substr($line, $rightone, $leftone - $rightone));
}
}
}
If you're looking just for the background (and they follow this order of options (url, repeat, rest)):
preg_match_all('~background:\s*url\(['"]?([^)]+)['"]?\)\s+no-repeat[^;]*;~i', $css, $reg);
print_r($reg[1]);
output:
Array
(
[0] => ../img/delete.png
[1] => ../img/pen.png
)
精彩评论