fork(1) download
  1. <?php
  2. $string = "I want to replace ' with \' but not \' since they are already escaped";
  3. $regex = "~(?<!\\\)'~";
  4.  
  5. echo preg_replace($regex, "\\'", $string);
  6.  
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
I want to replace \' with \' but not \' since they are already escaped