$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?
$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/'=>''));
Related
Goal -
convert a path: /aaaaa/bbbbb/ccccc/dddd
to a relative path (to the root): ../../../../
So far I've come up with this regex: /\/.+?\//
but this only produces: ..bbbbb..dddd because it is only matching every other pair of slashes, and also matching the slashes. I'm looking for something like a string split, but also replace.
All of my php code:
$pattern = '/\/.+?\//';
$path = '/aaaaa/bbbbb/ccccc/dddd';
echo preg_replace($pattern, '..', $path);
preg_replace('/\/{0,1}(\w+)\/{0,1}/', '../', $path);
This is working for me.
How about:
preg_replace(':/[^/]+:', '../', $path);
what about the below ?
$pattern = '/\//';
$path = '/aaaaa/bbbbb/ccccc/dddd';
preg_replace(array($pattern), array('..'), $path
My pages are www.example.com/somthing/types.html , www.example.com/somthing2/types.html this html files has a tag<a href="animals.html" . somthing,somthing2are files every file contains links in types.html and files like animals.html in the same folders.
when i get it with dom it shows only "animals.html" but i want to get "www.example.com/somthing/animals.html". how can i remove types.html from www.example.com/somthing/types.html and put www.example.com/somthing+/animals.html.
i just need a way to remove "types.html" from www.example.com/somthing/types.html.
keep the folder and remove the last part after this "/".
i dont know always last part (file name) so i need a way to remove last thing after last "/". sorry about my language problems.
str_replace('types.html' ,'','www.example.com/somthing/types.html');is for if i know the file name (types.html).
You can use this
preg_replace('/([^\/]*$)/', '', 'www.example.com/somthing/types.html');
This will replace all characters after the last /
Output will be www.example.com/somthing/
Try this
$string = 'www.examle.com/somthing/types.html';
echo preg_replace('#\/[^/]*$#', '', $string);
If you are considering the fast and the fastest you may consider this piece of code instead preg_replace
$url = explode("/",$url);
array_pop($url);
$url = implode("/",$url);
Again it is slighly faster so do as you please :).
Proof:
http://sandbox.onlinephpfunctions.com/code/5083e02d4f171502ef1e7c87c8dd9a957ee0d8fb
You can also do this the old fashion way:
$url = 'www.example.com/somthing/types.html';
$divided = explode( '/', $url );
array_pop( $divided );
$new_url = implode( '/', $divided );
I need a regular expression that would take the string after the last forward slash.
For example, considering I have the following string:
C:/dir/file.txt
I need to take only the file.txt part (string).
Thank you :)
You don't need a regex.
$string = "C:/dir/file.txt";
$filetemp = explode("/",$string);
$file = end($filetemp);
Edited because I remember the latest PHP spitting errors out about chaining these types of functions.
If your strings are always paths you should consider the basename() function.
Example:
$string = 'C:/dir/file.txt';
$file = basename($string);
Otherwise, the other answers are great!
The strrpos() function finds the last occurrence of a string. You can use it to figure out where the file name starts.
$path = 'C:/dir/file.txt';
$pos = strrpos($path, '/');
$file = substr($path, $pos + 1);
echo $file;
I am trying to sanitize a filename.
I would like to know of a way to remove all decimals from a files name except the last one. I need to keep the last one because the extension follows that.
EXAMPLE:
abc.def.ghij-klmnop.q234.mp3
This file should look like
abcdefghij-klmnopq234.mp3
Some extensions are longer than 3 characters.
You can use a regex with a positive lookahead. Like this:
$withdots = 'abc.def.ghij-klmnop.q234.mp3';
$nodots = preg_replace('/\.(?=.*\.)/', '', $withdots);
After executing the above, $nodots will contain abcdefghij-klmnopq234.mp3. The regular expression is basically saying match all periods that are followed by another period. So the last period won't match. We replace all matches with an empty string, and we're left with the desired result.
That should do it:
$file = 'abc.def.ghij-klmnop.q234.mp3';
$parts = pathinfo($file);
$filename = str_replace('.', '', $parts['filename']).'.'.$parts['extension'];
You could also do this, it should be faster then using pathinfo & str_replace.
$parts = explode('.', 'abc.def.ghij-klmnop.q234.mp3');
$ext = array_pop($parts);
$nodots = implode('', $parts) . '.' . $ext;
Assuming $s is the name of the file.
$s = (($i = strrpos($s, '.')) === false) ? $s :
str_replace('.','',substr($s,0,$i)).substr($s,$i);
string '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg' (length=85)
what i need is just
http://localhost/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
what is the best way doing it ? i mean useing strlen ? substr_replace ? substr ? im a bit confused what is the best way doing this? becouse there is many ways to do this.
edit* there is no newbie tag :|
// get from database red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
$image_path = $this->data['products'][0]['image_small'];
$exploded = end(explode('/', $image_path));
$myurl = DOMAIN;
$myfullurl = $myurl."/storage/".$exploded;
// it works!, but let see the comments maybe there is a better way :)
Here is how you can get the image part:
$str = '/home/adam/Projects/red/storag/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg';
$exploded = end(explode('/', $str));
echo $exploded;
Result:
22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
Now you can concatenate it with whatever eg:
$new_str = 'http://localhost/storage/' . $exploded;
echo $new_str;
Result:
http://localhost/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
And It is most likely you want to concatenate the image path with your document root which you do like this:
$img_path = $_SERVER['DOCUMENT_ROOT'] . $exploded;
The idea is that you explode the string with explode function by specifying / as delimiter. This gives you array, now you use the end function to get the ending part of the array which is your image actually.
If the path prefix represents your document root path, then you can do this to strip it:
$path = '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg';
$_SERVER['DOCUMENT_ROOT'] = '/home/adam/Projects/red/';
if (substr($path, 0, strlen($_SERVER['DOCUMENT_ROOT'])) === $_SERVER['DOCUMENT_ROOT']) {
$uriPath = substr($path, strlen(rtrim($_SERVER['DOCUMENT_ROOT'], '/')));
echo $uriPath;
}
I suggest you check if the string contains /home/adam/Projects/red, and if it does, you use substr to get the part after it, and you glue it with http://localost.
$path = '/home/adam/Projects/red/storage/*snip*.jpg';
$basePath = "/home/adam/Projects/red";
if (strpos($path, $path) !== false)
$url = 'http://localhost' . substr($path, strlen($basePath));
This one's pretty much the easiest
str_replace(
"/home/adam/Projects/red",
"http://localhost",
"/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg"
);
$string = '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg';
str_replace('/home/adam/Projects/red', 'http://localost', $string)