Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: Finding URLs in background-image CSS, having trouble

Here's my regex code:

preg_match_all('/background[-image]*:[\s]*url\(["|\']+(.*)["|\']+\)/', $css, $matches, PREG_SET_ORDER);

It looks for CSS that looks like this:

background:url('../blah.jpg');

My problem I'm having is some CSS I scrape looks like this:

background:transparent url('../blah.jpg');
background:transparent no-repeat url('../blah.jpg');

I'm no expert when it comes to regex, so I'm wondering how I can tell it to skip anything after the colon and before URL.

like image 502
dallen Avatar asked Mar 27 '12 15:03

dallen


People also ask

Why is my background image not working in CSS?

Make sure the image path is set correctly in the background-image url. Once you have made sure that your CSS file is linked correctly, also check that the image itself is set correctly. Again, you will want to open your code inspector in the browser to check.

Which property specifies the url of the image to be set as a background of an element?

The background-image property specifies an image to use as the background of an element.

Which CSS property will let you apply an image into the background of an element?

The background-image CSS property sets one or more background images on an element.

How to specify url value for background-image property?

The syntax to specify URL value for background-image property is If the size of background image and HTML element are not same, the background image is not resized to that of the HTML Element. If length or width of background image is greater than that of HTML Element, then the background image appears cropped.

How to display an image as background using CSS?

To display an image as background, set CSS background-image property with URL value. The syntax to specify URL value for background-image property is If the size of background image and HTML element are not same, the background image is not resized to that of the HTML Element.

How to fix background image URL not working in HTML?

Furthermore, you must include the link tag in the HTML file to fix a problem such as a background-image URL not working. The link tag should always be something like this <link rel=”stylesheet” href=”text/style.css”> inside the <head> tag.

How to scroll or fix the background image in CSS?

The background-attachment feature is used to scroll or fix the background image in CSS because the scroll is its default value. This property has the following values. Local: In this value, the background images are scrolled along with the content. Fix: Using this value fixes the background image with the page.


2 Answers

Ths should catch all the images unless I skipped anything.

preg_match_all('~\bbackground(-image)?\s*:(.*?)\(\s*(\'|")?(?<image>.*?)\3?\s*\)~i',$str,$matches);
$images = $matches['image'];
print_r($images);
like image 194
inhan Avatar answered Nov 15 '22 07:11

inhan


Try this:

preg_match_all('/background[-image]*:.*[\s]*url\(["|\']+(.*)["|\']+\)/', $css, $matches, PREG_SET_ORDER);
like image 39
robertvoliva Avatar answered Nov 15 '22 06:11

robertvoliva