Source Code:
(back to article)
<?php // Define a string with the full name $name = "John Smith"; // Split the full name into parts using a regular expression $name_parts = preg_split("/[\s,]+/", $name); // Assign the first name $first_name = $name_parts[0]; // Extract the last name $last_name = array_slice($name_parts, -1)[0]; // Output the first and last name echo "First name: " . $first_name . "\n"; echo "Last name: " . $last_name . "\n"; // Output: // First name: John // Last name: Smith ?>
Result:
Report an issue