4

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('../img/pen.png', '../img/delete.png')

3 Answers 3

3

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];
}
Sign up to request clarification or add additional context in comments.

Comments

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));
        }
    }
}

Comments

0

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
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.