So here is my code, and then I will explain what I am having trouble with:
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++;
}
What I want to do, is print the original $person, but with the $firstName and $lastName 's reversed. I'm able to replace the values and then print out each variable, but then it wouldn't have the same format as $person originally did unless I formatted each line. I was wondering if there was a way to do it without formatting each output to look identical to the original $person variable.
Thanks!
It looks as if Firstname and Lastname are always separated by whitespace and preceed the first colon character... if this is correct the above may be overkill. Try this:
echo $newPerson = (preg_replace("/(.*?)\s+(.*?)\:/", '$2,$1:', $person))."<br/>";