Template:Fname: Difference between revisions

From Robin's SM-201 Website
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:
<?php
function formatName(fullName) {
$fullName = {{{1}}};
  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
  }
}


// Split the full name into an array of words using space as the delimiter
// Example usage in HTML:
$nameParts = explode(" ", $fullName);
const originalName = "John Doe";
const formattedName = formatName(originalName);


// Check if there are at least two parts (first name and last name)
// Display the formatted name in an HTML element
if (count($nameParts) >= 2) {
document.getElementById('nameDisplay').textContent = formattedName;
    $firstName = $nameParts[0];
    $lastName = $nameParts[1]; // Assuming the last name is the second part


    // Construct the desired format "lname, fname"
// HTML structure example:
    $formattedName = $lastName . ", " . $firstName;
// <p id="nameDisplay"></p>
    echo $formattedName; // Output: Doe, John
} else {
    echo "Invalid name format.";
}
?>

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:

//