3v4l.org

run code in 300+ PHP versions simultaneously
<?php $names = [ "Mr. Kiefer William Frederick Dempsey George Rufus Sutherland Sr.", "Carolina Maria de Jesus", "Kiefer William Frederick Dempsey George Rufus Sutherland Jr.", "Mrs. Carolina Maria de Jesus"]; foreach($names as $name) { preg_match('/(?:\w+\. )?(\w+).*?(\w+)(?: \w+\.)?$/', $name, $result); echo $result[1] . ' ' . $result[2] . ' = ' . $result[1][0].$result[2][0] . PHP_EOL; }

preferences:
25.08 ms | 404 KiB | 5 Q