I will be the best
I will be the best

Reputation: 59

php preg_match css source code

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

Answers (2)

Avinash Raj
Avinash Raj

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){

DEMO

Update:

preg_replace('~(background-image:url\(["\']?)(?=\/)~', '\1website.com', $data);

DEMO

Upvotes: 1

Ganesan Karuppasamy
Ganesan Karuppasamy

Reputation: 367

U can try this code

if (preg_match_all('/url\(\s*[\'"]?([^\'")]+)[\'"]?\s*\)/i', $date, $matches)) {
  echo 'yes';
} else {
  echo 'no';
}

Upvotes: 0

Related Questions