1

I'm using file_get_contents() to get source code of a page but I fail in some websites that uses CSS like this:

background: url("/media/image")
background: url(/media/image)
background: url('/media/image')
// etc...

Now, I want to know how can I edit that CSS and add my website, so it will look like this:

background: url("http: //example.com/media/image")
background: url('http: //example.com/media/image')
background: url(http://example/media/image)
// etc...

. Here is my code:

$regex = "-(src\s*=\s*['\"])(((?!'|\"|http://|https://|//).)*)(['\"])-i"; 

I'm doing this on the src attribute of an HTML tag. I hope I'm helping

10
  • I'm not sure what you mean, your comments are getting confusing. I thought this is what you were trying to accomplish echo preg_replace('~url\(("|\')?/?(.*?)(?:\1)?\)~', 'url($1http://www.example.com/$2$1)', $css) . "\n";, no? Where $css is one of your three examples. Commented Jun 30, 2015 at 1:37
  • can u tell me what this code is doing? Commented Jun 30, 2015 at 1:38
  • @chris85 -(src\s*=\s*['\"])(((?!'|\"|http://|https://|//).)*)(['\"])-i how can I add href attribute to src please? Commented Jun 30, 2015 at 1:47
  • I want this regex code -(src\s*=\s*['\"])(((?!'|\"|http://|https://|//).)*)(['\"])-i to be working with href attribute too not only src :/ Commented Jun 30, 2015 at 1:48
  • 1
    Perhaps you should clerify what exactly you want? What are you trying to accomplish? Are you trying to edit the source code of what's imported with file_get_contents()? To me it pretty much sounds like you're importing someone elses website and replace the stuff you can't import. Something smells a bit to much like phishing here.. Commented Jun 30, 2015 at 2:03

1 Answer 1

2

This $regex will replace the background: url(["|']?$URL["|']?) with ["|']?$img["|']?:

$homepage = file_get_contents('http://www.example.com/');
$img = "http://placehold.it/350x150";

$regex = '/(background: url\((["|\']?))(.+)(["|\']?\))/';
$replacement = "$1$img$4";
$homepage = preg_replace($regex, $replacement, $homepage);

echo $homepage;
Sign up to request clarification or add additional context in comments.

5 Comments

This regex: $regex = '/(background-image: url\(\')(.+)("|\'\))/'; with this replacement: $replacement = "$1$img$3"; worked better for me. Basically leaves background-image: url(' and ') in place and insterts the URL between, not touching the quotation marks.
@GiffordN. Is that regex meets the OP requirements?
Probably not for the op, that's why I left as comment instead of editing your post. Unfortunately your regex caused a CSS error because it retains the first ' but not the second ', just the ) so it broke the CSS. I was just addressing that issue for whoever may experience a similar problem using your code, since this is how I fixed the issue.
@GiffordN. I think your url\(\')(.+)("|\'\) should become url\(\'(.+)\'\).
@GiffordN. You're welcome, but you should give an upvote if you think the post have helped you..

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.