Reputation: 11
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
Upvotes: 1
Views: 920
Reputation: 4479
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;
Upvotes: 2