0

This Regex:

$pattern = '/url\(([^)]+)\)/';

matches everything between url( ... ) in CSS files. The problem: If there are quotes, like this: url('...'), they occur in the returned phrase as well.

How to return a phrase without quotes, if there are any?

2
  • Applies to single and double quotes.... Commented Oct 28, 2014 at 10:41
  • The same story over and over again. Someone asks "how to parse <some structured language> with regexes". Someone comments "regexes are not the tool, use a parser". Someone answers with a half-baked regex that kinda works for a particular case. Many thanks, answer accepted, end of story. Commented Oct 28, 2014 at 10:58

1 Answer 1

1
url\(['"]?([^)]+?)['"]?\)

This should do it for you.Make ' or " optional.

See demo.

http://regex101.com/r/sU3fA2/27

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

12 Comments

It's OK, it is our own project so we take care about such things. The answer is absolutely perfect for my needs. Will accept it as soon as SO allows me to :)
@BarthZalewski you can refer to demo as well.Now it handles all the cases.
@AvinashRaj now it takes care of your concern too :)
I think exactly like vks. If it matches misspelled URI-s, it's even better. It's not a bug, it's a feature.
Oh, I forgot something. You might need to use str_replace() after all. demo. Here's a regex that works and detects even broken quotes.
|

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.