fork download
  1. <?php
  2.  
  3. $str = 'Hello World!';
  4. echo preg_replace('~\b(\p{L})\p{L}*(\p{P}?)~u', '$1$2', $str) . "\n";
  5.  
  6. $res = [];
  7. preg_replace_callback('~\b(\p{L})\p{L}*(\p{P}?)~u', function($m) use (&$res) {
  8. $res[] = $m[1].$m[2];
  9. return '';
  10. }, $str);
  11. print_r(implode(" ", $res));
Success #stdin #stdout 0.02s 24180KB
stdin
Standard input is empty
stdout
H W!
H W!