How to efficiently replace quotes (or wrapping symbols) in the part of the dynamic html string?
I have a html string like -
...style="background-position: left top; background-image: url("http://localhost/abc/{DYNAMIC CONTENT}") etc...
What would be fast and ef开发者_运维知识库ficient way to replace double quotes inside url("")
part with single ones if there are double quotes after style=" I don't need to replace quotes everywhere, just in that certain place. Thank You.
Use a regex (match with enough context for less false positives):
$src = preg_replace(
'#(style="[^"]+:\s*url) [(]" ([^"]+) "[)]#x',
"$1('$2')",
$src
);
Though that only replaces one double quotes " "
pair per inline style=
. So you might have to run it twice over your source if you expect multiple malformed attributes/url("..") expressions.
精彩评论