remove characters from variable
$dir = '/web99/web/direcotry/filename';
I need to drop '/web99/web/' from t开发者_C百科he 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?
$dir = preg_replace('$^/web99/web/$', '', $dir);
Use str_replace
:
$dir = str_replace('/web99/web/', '', $dir);
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);
I would use strtr(), so if you need other string replacements, editing will be easy :
$trimmed = strtr($dir, array('/web99/web/'=>''));
精彩评论