fork download
  1. <?php
  2.  
  3. $text = "[blue]My [black]house is [blue]very[/blue] beautiful[/black] today[/blue]";
  4.  
  5. $regex = '~(\[ ( (?>[^\[\]]+) | (?R) )* \])~x';
  6.  
  7.  
  8.  
  9. $replacements = array( "blue" => "<bleu>",
  10. "black" => "<noir>",
  11. "/blue" => "</bleu>",
  12. "/black" => "</noir>");
  13.  
  14. $text = preg_replace_callback($regex,
  15. function($match) use ($replacements) {
  16. return $replacements[$match[2]];
  17. },
  18. $text);
  19.  
  20. echo $text;
  21. ?>
Success #stdin #stdout 0s 52488KB
stdin
Standard input is empty
stdout
<bleu>My <noir>house is <bleu>very</bleu> beautiful</noir> today</bleu>