所以这是我的代码,然后我将解释我遇到的问题:
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变量相同。
谢谢!
答案 0 :(得分:0)
看起来Firstname和Lastname总是以空格分隔并且在第一个冒号前面...如果这是正确的,则上面的内容可能有点过分。试试这个:
echo $newPerson = (preg_replace("/(.*?)\s+(.*?)\:/", '$2,$1:', $person))."<br/>";