Template:Fname: Difference between revisions
Jump to navigation
Jump to search
(Created page with "<?php $fullName = {{{1}}}; // Split the full name into an array of words using space as the delimiter $nameParts = explode(" ", $fullName); // Check if there are at least two parts (first name and last name) if (count($nameParts) >= 2) { $firstName = $nameParts[0]; $lastName = $nameParts[1]; // Assuming the last name is the second part // Construct the desired format "lname, fname" $formattedName = $lastName . ", " . $firstName; echo $formattedName...") |
No edit summary |
||
| Line 1: | Line 1: | ||
function formatName(fullName) { | |||
const nameParts = fullName.split(' '); // Split the full name into an array of parts | |||
if (nameParts.length >= 2) { // Ensure there are at least two parts (first and last name) | |||
const firstName = nameParts[0]; | |||
const lastName = nameParts[nameParts.length - 1]; // Get the last part as the last name | |||
return `${lastName}, ${firstName}`; // Construct the new format | |||
} else { | |||
return fullName; // Return the original name if it doesn't fit the expected format | |||
} | |||
} | |||
// | // Example usage in HTML: | ||
const originalName = "John Doe"; | |||
const formattedName = formatName(originalName); | |||
// | // Display the formatted name in an HTML element | ||
document.getElementById('nameDisplay').textContent = formattedName; | |||
// HTML structure example: | |||
// <p id="nameDisplay"></p> | |||
Latest revision as of 13:47, 10 November 2025
function formatName(fullName) {
const nameParts = fullName.split(' '); // Split the full name into an array of parts
if (nameParts.length >= 2) { // Ensure there are at least two parts (first and last name)
const firstName = nameParts[0];
const lastName = nameParts[nameParts.length - 1]; // Get the last part as the last name
return `${lastName}, ${firstName}`; // Construct the new format
} else {
return fullName; // Return the original name if it doesn't fit the expected format
}
}
// Example usage in HTML: const originalName = "John Doe"; const formattedName = formatName(originalName);
// Display the formatted name in an HTML element document.getElementById('nameDisplay').textContent = formattedName;
// HTML structure example:
//