Here's some sample code that should work OK:
<?php
function getCanonicalName($name) {
// Check for the existance of a comma and swap 'elements' if necessary.
if(strpos($name, ',') !== false) {
list($surname, $forename) = explode(',', $name);
$name = $forename . ' ' . $surname;
}
// Trim the name.
return trim($name);
}
// Test data and file I/O.
$outputData = '';
$testData = array('Obama, Barak', 'Federer, Roger', 'John Parker');
foreach($testData as $name) {
$outputData .= getCanonicalName($name) . "\n";
}
file_put_contents('current_name.txt', $outputData, FILE_APPEND);
?>
Incidentally, this (like all of the solutions currently attached to your question) will cause data loss if there's more than one comma in $name. As such, if this is possible you should update getCanonicalName to cater for this eventuality.