Reputation: 191179
Let's say I have a windows path name "E:\Dropbox\b\c\d", when I want to change this path to UNIX with some modifications, I can use this python code.
x = "E:\\Dropbox\\b\\c\\d"
print "/Users/prosseek/" + '/'.join(x.split('\\')[1:])
>> /Users/prosseek/Dropbox/b/c/d
What could be the equivalent code in PHP?
Upvotes: 0
Views: 84
Reputation: 2490
A pretty much exact equivalent of that Python code would be:
$x = 'E:\\Dropbox\\b\\c\\d';
echo "/Users/prosseek" . implode('/', explode('\\', substr($x, 2)));
Upvotes: 1
Reputation: 198247
If you're looking for the exact PHP equivalent:
$x = 'E:\Dropbox\b\c\d';
$parts = explode('\\', $x);
array_shift($parts);
print "/Users/prosseek/" . implode('/', $parts);
However, this does not create the output you wanted. Demo.
Upvotes: 1
Reputation: 11546
Here you go. You could use explode
and implode
, but there's no real reason to get fancy and use arrays when this is just a basic string operation:
$x = 'E:\Dropbox\b\c\d';
echo '/Users/prosseek/' . str_replace('\\', '/', substr($x, strpos($x, '\\') + 1));
Upvotes: 2