0
$str = "Test Artist Test - Test Title Test";  
$trackinfo = preg_split('/-/', $str);

exec('metamp3 --title '.$trackinfo[1].' --artist '.$trackinfo[0].' track.mp3');

This is a cut of the code I am using, basically I'm obviously doing something wrong (I'm quite new to PHP and don't really understand some of the conventions used. When I run this line, from what I can see it would only put Test and Test for the title and artist (like it is only taking the first word from the string) but if I was to do something like

print_r $trackinfo;
print $trackinfo[1];
print $trackinfo[0];

I can clearly see that the split string is formatted correctly, I was wondering if someone could explain what exactly is going on here and how I would go about fixing it?

Thanks!

2 Answers 2

3

That's not a PHP problem. You're passing following string to exec:

metamp3 --title Test Title Test --artist Test Artist Test track.mp3

But metamp3 program will take only first word for each parameter. The command should look like metamp3 --title "Test Title Test" ... (parameter value in quotes).

There is function in PHP to solve this problem: escapeshellarg. Here is how your code can look like:

exec(
    'metamp3 --title '.escapeshellarg($trackinfo[1]).
    ' --artist '.escapeshellarg($trackinfo[0]).' track.mp3'
);
Sign up to request clarification or add additional context in comments.

1 Comment

You're awesome sir, I thought (hoped) there would be a way to tackle this but I wasn't aware of a solution. Thank you, so much!
1

Try to add quotes:

exec('metamp3 --title "'.$trackinfo[1].'" --artist "'.$trackinfo[0].'" track.mp3');

2 Comments

This will fail if there are " symbols in song name.
@Ivan Nevostruev, yeah, you are correct! Your decision is better.

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.