PHP字符串编辑 - 简单

I am creating a website that pulls data from a CSV file and then displays the album artwork that matches the album and artist however I've hit a snag... the file presents artists as either a band (example: U2) or the artist (Dylan, Bob) the later does not work with the api.

I've tried to no success to edit the string so if it sees "," it will then rearrange the artist from last, first to first last

Does anyone know a solution to this?

Cheers!

explode() on , (comma-space) into variables $last, $first. If $first is nonempty, rearrange them. Otherwise, just output the original $fullname.

$fullname = "Dylan, Bob";

$parts = explode(", ", $fullname);
$first = isset($parts[1]) ? $parts[1] : NULL;
$last = $parts[0];
if (!empty($first)) {
  $output = "$first $last";
}
else $output = $fullname;

A regex might work for you:

if( preg_match("#(\w+),\s?(\w+)#", $artist) )
    $artist = preg_replace("#(\w+),\s?(\w+)#", "\2 \1", $artist)
$string = "Dylan, Bob";
$fixed = preg_replace('/^(.*), (.*)$/', '$2 $1' , $string);