您所拥有的标记没有定义一种常规语言,因此,正则表达式不能很好地实现这一点。我认为你能做的最好的事情就是移除所有的标签(用其他东西替换它们),用“birds”替换“words”,然后把内容放回原处。
$str = preg_replace_callback('!\[one\].*?\[/one\]!s', 'hash_one', $input);
$str = str_replace('words', 'birds', $str);
$output = preg_replace_callback('!:=%\w+%=:!', 'replace_one', $str);
$hash = array();
function hash_one($matches) {
global $hash;
$key = ':=%' . md5($matches[0]) . '%=:';
$hash[$key] = $matches[0];
return $key;
}
function replace_one($amtches) {
global $hash;
$ret = $hash[$matches[0]];
return $ret ? $ret : $matches[0];
}