Mike
Mike

Reputation: 2225

remove characters from variable

$dir = '/web99/web/direcotry/filename';

I need to drop '/web99/web/' from the directory on the fly.

I tried:

$trimmed = ltrim($dir, "/web99/web/");

however if a directory started with a w,e,b,9 the first letter of the directory was cut off.

How can I drop only what I want and not every character like that?

Upvotes: 1

Views: 1680

Answers (4)

sglessard
sglessard

Reputation: 3156

I would use strtr(), so if you need other string replacements, editing will be easy :

$trimmed = strtr($dir, array('/web99/web/'=>''));

Upvotes: 0

sdleihssirhc
sdleihssirhc

Reputation: 42496

You could just use str_replace if you know that it will never be anywhere else in the path:

$trimmed = str_replace('/web99/web/', '', $dir);

If you wanted to be really safe, you could use preg_replace to get the beginning of the string:

$trimmed = preg_replace('~^/web99/web/~', '', $dir);

Upvotes: 0

Oswald
Oswald

Reputation: 31637

$dir = preg_replace('$^/web99/web/$', '', $dir);

Upvotes: 2

Jon
Jon

Reputation: 437326

Use str_replace:

$dir = str_replace('/web99/web/', '', $dir);

Upvotes: 0

Related Questions