php preg_replace regex replace string between two string

后端 未结 4 929
余生分开走
余生分开走 2020-12-22 04:28

I have the following problem: I want to replace (in php) a special character, but only if it\'s between two other characters. It tried to find a solution with with preg_repl

4条回答
  •  遥遥无期
    2020-12-22 05:01

    A simple understandable solution could be the use of preg_replace_callback:

    $str = preg_replace_callback('/"[^"]+"/',
           function ($m) { return str_replace(";", ":", $m[0]); },
           $str);
    

    "[^"]+" captures the quoted stuff to $m[0] where ; is replaced by :

    See test at eval.in (link will expire soon)

提交回复
热议问题