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.
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.
The background-image property specifies an image to use as the background of an element.
The background-image CSS property sets one or more background images on an element.
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.
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.
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.
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.
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);
Try this:
preg_match_all('/background[-image]*:.*[\s]*url\(["|\']+(.*)["|\']+\)/', $css, $matches, PREG_SET_ORDER);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With