如何存储一个名字

时间:2016-01-07 17:30:15

标签: php

我试图将一个名字分割为firstName,这样我就可以将它存储在一个表格中。我是php和编程的初学者。感谢任何帮助,谢谢。

$Names = 'Donny P, Raph Rsa, Leo Old, Laugh Orange';

foreach ($fullnames as $fullname) {
    $nameSplit = explode(" ", $fullname);

    if ($Names == empty($nameSplit[0]) || empty($Names)){
        echo 'No First Name Here Or No Name At All';
        echo '<br>';
        echo '<br>';
    } elseif ($Names == empty($nameSplit[1])){
        echo 'No Last Name Here';
        echo '<br>';
        echo '<br>';
    } else{
        echo 'First Name: ' . $nameSplit[0];
        echo '<br>';
        echo 'Last Name: ' . $nameSplit[1];
        echo '<br>';
        echo 'Email Address: ' . $nameSplit[0] . $email;
        echo '<br>';
        echo '<br>';
    }

}

以下是我在逻辑上的想法,我知道它不是有效的代码,但它有助于我的想法。

foreach ($nameSplit as $nameSplits) {
store nameSplit[0] as $firstName;
store nameSplit[1] as $lastName;
store nameSplit[0] as $emailUser;
}

1 个答案:

答案 0 :(得分:0)

我建议你首先爆炸逗号,然后遍历每一个逗号。

这样的事情:

$exp = explode(",", $Names);

foreach($exp as $names){
    $nameSplit = explode(" ", $names);
    if($nameSplit[0] == ""){ $s = 1; }else{ $s = 0; }

    $firstName = $nameSplit[$s];
    $lastName = $nameSplit[$s+1];
    //or you can loop through any other names here starting with the first position
}