I am very new to any coding, so please bear with me if this seems basic.
I am trying to allocate the appropriate greeting in an email with the variable $fname. The data that the name values originate from is a simple, single string. The possible variations in format are therefore numerous.
In this particular application, the details are mostly written as either:
-Mr John Smith
-Mr Smith
-John Smith
-John
I therefore need to evaluate the method used to enter the name before I can allocate it to the variable.
I have written the follow code, but online checking throws up "an unexpected 'else'".
It all seems right to me and the online examples I have looked at seem to follow a similar order.
Any help greatly appreciated!
Thanks,
Rob.
<?php
// splits the name field into parts
$names = explode(" ", $client);
// works out if the name field starts with a salutation
if ($names[0] == ("Mr" || "Mrs" || "Ms" || "Mr." || "Mrs." || "Ms.")) {
// decides if it is written as "Salutation, surname"
if (empty($names[2])) {
$lname = $names[1];
$salname = array($names[0], $names[1]);
$fname = implode(" ", $salname);
// evaluates that if it starts with a salutation and has 3 names, it must then be "salutation firstname lastname"
} else {
$fname = $names[1];
$lname = $names[2];
// but if there was no salutation and only 2 names were given...
} else {
if (empty(names[2])) {
$fname = $names[0];
$lname = $names[1];}
// otherwise it must be "first, middle, last"
} else {
$fname = $names[0];
$lname = $names[3];
}
?>
Aucun commentaire:
Enregistrer un commentaire