0

これが私のコードです。それから私が問題を抱えていることを説明します:

foreach($people as $person){
    // counter for each person
    $counter = 0;
    // get variables for each item
    list($fullName,$phoneNumber,$address,$birthday,$salary) = explode(':', $person);
    // get variables for first and last name
    list($firstName, $lastName) = explode(" ", $fullName);
    // get variables for phone numbers
    list($areaCode, $middleDigits, $lastDigits) = explode('-',$phoneNumber);
    //get variables for address
    list($street,$city,$statezip) = explode(", ", $address);
    // get variables for state and zip separately
    list($state,$zipCode) = explode(" ", $statezip);


    // print file with the first and last names reversed
    $tempFName = $firstName;
    $tempLName = $lastName;
    echo $newPerson = (preg_replace("/($tempLName)($tempFName)/", $firstName, $lastName, $person))."<br/>";

    $counter++;
}

私がやりたいのは、元の$ personを印刷することですが、$firstNameと$lastNameは逆になっています。値を置き換えてから各変数を出力することはできますが、各行をフォーマットしない限り、$personが最初に行ったのと同じフォーマットにはなりません。元の$person変数と同じように各出力をフォーマットせずにそれを行う方法があるかどうか疑問に思いました。

ありがとう!

4

1 に答える 1

0

Firstname と Lastname は常に空白で区切られ、最初のコロン文字の前にあるように見えます...これが正しい場合、上記はやり過ぎかもしれません。これを試して:

echo $newPerson = (preg_replace("/(.*?)\s+(.*?)\:/", '$2,$1:', $person))."<br/>";
于 2012-04-27T04:28:25.903 に答える