开发者

Preg Replace code for json string

开发者 https://www.devze.com 2023-03-26 14:11 出处:网络
I want to replace all sequential commas ,, with {\"v\":\"\"} for e开发者_C百科xample ,,,,, would also be replaced.

I want to replace all sequential commas ,, with {"v":""} for e开发者_C百科xample ,,,,, would also be replaced.

I've tried

preg_replace('/,+/',',{"v":""},', $string);

Though I probably got something wrong. Any help would be appreciated. Thank you.


This should do what you want:

preg_replace('/,{2,}/', ', {"v":""}', $string);

However, a simple regular expression isn't the best tool for this job because it will break as soon as there is a string containing multiple commas.

0

精彩评论

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

关注公众号