fork download
  1. <?php
  2. $str = 'This is "some" {"text" : \'duudu\', \'duuue\' : "yey" } "and" {"some", "other"} "text"';
  3. echo "opt1: " . preg_replace_callback('~({[^}]*})~', function($m) {
  4. return preg_replace('~(?<!\\\\)[\'"]~', '\"', $m[1]);
  5. }, $str) . "\n";
  6.  
  7.  
  8. echo "opt2: " . preg_replace('~[\'"](?!(?:[^{}]*{[^{}]*})*[^{}]*$)~', '\"' , $str) . "\n"
  9. ?>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
opt1: This is "some" {\"text\" : \"duudu\", \"duuue\" : \"yey\" } "and" {\"some\", \"other\"} "text"
opt2: This is "some" {\"text\" : \"duudu\", \"duuue\" : \"yey\" } "and" {\"some\", \"other\"} "text"