prosseek
prosseek

Reputation: 191179

Changing path information with PHP

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

Answers (4)

Michał Wojciechowski
Michał Wojciechowski

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

hakre
hakre

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

brianreavis
brianreavis

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

genesis
genesis

Reputation: 50982

$str = "E:\Dropbox\b\c\d";
$unix = str_replace('E:', '/Users/prosseek', $str);
$unix = str_replace('\\', '/', $unix); 

demo

Upvotes: 2

Related Questions