1

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)

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

11 Comments

it replaces urls that start with two slashes not one
are you trying to match or replace? pls provide expected output.
I'm trying to to preg_replace using preg_match as a condition
just post the expected output you would get after preg_replace.
I want to replace background-image and add my url if the background-image:url('/hamza.png'); start with one slash of course
|
0

U can try this code

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

Comments

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.