Reputation: 59
I have the following code:
$date =
"<style>
background-image:url(\"/hamza.png\");
background-image:url(/hamza.png);
background-image:url('/hamza.png');
</style>";
I want to usepreg_match
to check if the url start with a slash.
This is what I have tried:
if (preg_match('url\(("|\'|)(?:\/)\1\)', $data) {
echo 'yes';
} else {
echo 'no';
}
It always gives me "no". I would like to know what this code is for : (https?:\s*)?//.*?\1)
Upvotes: 1
Views: 951
Reputation: 174826
You forget to use delimiters and also you need to match the chars which exists inside the brackets.
if(preg_match('~url\((["\']?)/[^)]*\1\)~', $data){
Update:
preg_replace('~(background-image:url\(["\']?)(?=\/)~', '\1website.com', $data);
Upvotes: 1
Reputation: 367
U can try this code
if (preg_match_all('/url\(\s*[\'"]?([^\'")]+)[\'"]?\s*\)/i', $date, $matches)) {
echo 'yes';
} else {
echo 'no';
}
Upvotes: 0