fork download
  1. <?php
  2.  
  3. $re = '/\b((?:Apartment|Apt|Block|Department|Lot|Number|Villa)\s*)([0-9]+)/i';
  4. $str = "23 St John Apt 92 rer 4, Wellington Country Block 5 No value test 54545 tt 232";
  5. $result = preg_replace_callback($re, function($m){
  6. return $m[1] . str_pad($m[2],4,"0", STR_PAD_LEFT);
  7. }, $str);
  8. echo $result;
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
23 St John Apt 0092 rer 4, Wellington Country Block 0005 No value  test 54545 tt 232