开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜