$str = "the boy like to the play football [[ the very much the friend ]] thank the you";
preg_match('/\[\[.*\]\]/',$str,$matches);
$patterns = array('/the/','/\[\[.*\]\]/');
$replacements = array('a',$matches[0]);
$new_str = preg_replace($patterns, $replacements, $str);
echo $new_str;
$str = "the boy like to [[ the friend ]] the play football [[ the very much the friend ]] thank the you";
$reg = '/\[\[[^\]]*\]\]/';
preg_match_all($reg,$str,$matches);
$matches=$matches[0];
$strMatches=join('|',$matches);
$strMatches=str_replace('the','a',$strMatches);
$replacedMatches=explode('|',$strMatches);
$patterns = array_merge(array('the'),$replacedMatches);
$replacements = array_merge(array('a'),$matches);
$new_str =str_replace($patterns, $replacements, $str);
echo $new_str;
$reg = '/\[\[[^\]]*\]\]/';
$str = "the boy like to [[ the friend ]] the play football [[ the very much the friend ]] thank the you";
$strReplaced=str_replace('the','a',$str);
preg_match_all($reg,$str,$matches);
preg_match_all($reg,$strReplaced,$replacedMatches);
$patterns=$replacedMatches[0];
$replacements=$matches[0];
$new_str=str_replace($patterns, $replacements, $strReplaced);
echo $new_str;
ผลลัพธ์ที่ได้
the {{fast|speedy|quick}} black {{fox|wolf}} {{bounded|hopped|skipped|jumped}} over {{a|the}} {{tired|lazy}} dog
แค่เอาผลลัพธ์นี้มา รีเพลซ เครื่องหมายกลับมาเป็น [[ ]] เหมือนเดิม
แต่ผมไม่แน่ใจว่ามันจะครอบคลุมถูกต้องจริงๆมั้ย