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; }
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.14, 8.4.1
Kiefer Sutherland = KS Carolina Jesus = CJ Kiefer Sutherland = KS Carolina Jesus = CJ

preferences:
79.04 ms | 407 KiB | 5 Q