- preg_replace_callback: documentation ( source)
- str_replace: documentation ( source)
<?php
$str = 'abc<abc>abc>';
$str = preg_replace_callback('/(<)[^>]++>|[^<]++|.++/', function ($m) {
return str_replace('a', isset($m[1]) ? 'c' : 'b', $m[0]);
}, $str);
echo $str;