0

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!

6
  • 2
    rubs crystal ball line 12, character 5 needs to be upper case! In seriousness though, we'll need to see your code before we can give any real help. Commented Nov 28, 2011 at 14:16
  • 1
    Do you mean you can't rearrange the string, or it still doesn't work after you have rearranged it? Commented Nov 28, 2011 at 14:16
  • You need to provide more information. Is the CSV file correct or do the additional , result in a problem? You mentioned an API...do you mean PHP or are you using some library? Commented Nov 28, 2011 at 14:17
  • please improve your question, not clear. Commented Nov 28, 2011 at 14:17
  • I hope your CSV is quoting special characters or you'll never get it to work Commented Nov 28, 2011 at 14:18

3 Answers 3

2

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;
Sign up to request clarification or add additional context in comments.

3 Comments

$first will always be populated because (annoyingly) list() populates from right to left. So you would need to do if ($last). I think.
@DaveRandom Yes, looks to be so. I made some changes accordingly.
This seems to be the best one so far! Thanks for your help!
1

A regex might work for you:

if( preg_match("#(\w+),\s?(\w+)#", $artist) )
    $artist = preg_replace("#(\w+),\s?(\w+)#", "\2 \1", $artist)

Comments

1
$string = "Dylan, Bob";
$fixed = preg_replace('/^(.*), (.*)$/', '$2 $1' , $string);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.