简体   繁体   中英

PHP escape unescaped quote marks

I know I can use the following to replace " with \\":

str_replace('"', '\"', $string);

But if some quotemarks are already escaped the above code breaks things as it replaces \\" with \\ \\".

So is there any way I can search and replace " only and ignore \\"?

Try something like this:

preg_replace('/(?<!\\)"/', '\"', $data);

Honestly though, escaping in this way is ugly. And if you need this, you are already doing something wrong.

You should highly consider refactoring your code to use proper escaping functions for what you are trying to achieve and so you won't need to worry about double-escaping :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM