Lewis
Lewis

Reputation: 137

PHP string whitespace formatting

$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!

Upvotes: 0

Views: 128

Answers (2)

Ivan Nevostruev
Ivan Nevostruev

Reputation: 28723

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'
);

Upvotes: 3

Karolis
Karolis

Reputation: 9562

Try to add quotes:

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

Upvotes: 1

Related Questions