fork download
  1. <?php
  2.  
  3. function displayuser($s)
  4. {
  5. $arr = ['77' => 'Arthur', '66' => 'John', '59' => 'Smith'];
  6. return $arr[$s];
  7. }
  8. function displayaccount($s)
  9. {
  10. $arr = ['28' => 'ACCOUNT'];
  11. return $arr[$s];
  12. }
  13.  
  14. $text = "[userid=77] has created a new task. [userid=59] and [userid=66] is are subscribed. This task is assigned to [accountid=28]";
  15. print "Before: $text\n";
  16. $text = preg_replace_callback('@\[userid=(\d+)]|\[accountid=(\d+)]@', function($m) {
  17. return !empty($m[1]) ? displayuser($m[1]) : displayaccount($m[2]);
  18. }, $text);
  19. print "After: $text\n";
Success #stdin #stdout 0.01s 82944KB
stdin
Standard input is empty
stdout
Before: [userid=77] has created a new task. [userid=59] and [userid=66] is are subscribed. This task is assigned to [accountid=28]
After: Arthur has created a new task. Smith and John is are subscribed. This task is assigned to ACCOUNT