3v4l.org

run code in 500+ PHP versions simultaneously
<?php function split_name($fullname) { $firstname = ""; $surname = ""; $pieces = explode(" ", $fullname); foreach ($pieces as $name) { if (ctype_upper(filter_var(str_replace("'", "", $name), FILTER_SANITIZE_STRING))) $surname .= $name." "; else $firstname .= $name. " "; } return array("firstname" => $firstname, "surname" => $surname); } $names = array("Jack David SMITH", "Pietro DE GIOVANNI", "Jane JOHNSON", "Matthew O'RILEY", "John-Paul JOHNSON"); $split = array_map("split_name", $names); print_r($split);
Output for 8.5.0 - 8.5.3
Deprecated: Constant FILTER_SANITIZE_STRING is deprecated since 8.1, use htmlspecialchars() instead in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated since 8.1, use htmlspecialchars() instead in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated since 8.1, use htmlspecialchars() instead in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated since 8.1, use htmlspecialchars() instead in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated since 8.1, use htmlspecialchars() instead in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated since 8.1, use htmlspecialchars() instead in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated since 8.1, use htmlspecialchars() instead in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated since 8.1, use htmlspecialchars() instead in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated since 8.1, use htmlspecialchars() instead in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated since 8.1, use htmlspecialchars() instead in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated since 8.1, use htmlspecialchars() instead in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated since 8.1, use htmlspecialchars() instead in /in/JXp2s on line 9 Array ( [0] => Array ( [firstname] => Jack David [surname] => SMITH ) [1] => Array ( [firstname] => Pietro [surname] => DE GIOVANNI ) [2] => Array ( [firstname] => Jane [surname] => JOHNSON ) [3] => Array ( [firstname] => Matthew [surname] => O'RILEY ) [4] => Array ( [firstname] => John-Paul [surname] => JOHNSON ) )
Output for 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18
Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /in/JXp2s on line 9 Deprecated: Constant FILTER_SANITIZE_STRING is deprecated in /in/JXp2s on line 9 Array ( [0] => Array ( [firstname] => Jack David [surname] => SMITH ) [1] => Array ( [firstname] => Pietro [surname] => DE GIOVANNI ) [2] => Array ( [firstname] => Jane [surname] => JOHNSON ) [3] => Array ( [firstname] => Matthew [surname] => O'RILEY ) [4] => Array ( [firstname] => John-Paul [surname] => JOHNSON ) )
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
Array ( [0] => Array ( [firstname] => Jack David [surname] => SMITH ) [1] => Array ( [firstname] => Pietro [surname] => DE GIOVANNI ) [2] => Array ( [firstname] => Jane [surname] => JOHNSON ) [3] => Array ( [firstname] => Matthew [surname] => O'RILEY ) [4] => Array ( [firstname] => John-Paul [surname] => JOHNSON ) )

preferences:
89.62 ms | 2295 KiB | 4 Q