$urlPath = "http://localhost:8000/img/mercedes-benz-a-class-image.png";
$imgUrlPath = "http://localhost:8000/img/";
$imageName = ltrim($urlPath, $imgUrlPath);
And I have ercedes-benz-a-class-image.png without first m.
$imageName = preg_replace($imgUrlPath, "", urlPath);
It's not work...
How do I get a line mercedes-benz-a-class-image.png?
Just use str_replace
$urlPath = "http://localhost:8000/img/mercedes-benz-a-class-image.png";
$imgUrlPath = "http://localhost:8000/img/";
$imageName = str_replace($imgUrlPath, "", $urlPath);
Involving regexes just messes things up for something this simple. From the documentation:
If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of preg_replace().
To get file name from path, You can simply use basename()
$urlPath = "http://localhost:8000/img/mercedes-benz-a-class-image.png";
$imageName = basename($urlPath);
Live Demo
And to get Directory name use dirname
$urlPath = "http://localhost:8000/img/mercedes-benz-a-class-image.png";
echo $dirName = dirname($urlPath);
Live Demo
Edit : from your comment
You can't have following character in your file name
Related
I have a variable that stores the location of a temp file:
$file = 'C:\xampp\htdocs\temp\filename.tmp';
How can I explode all this to get filename (without the path and extension)?
Thanks.
Is not the best code but if you confident that this path will be similar and just file name will be different you can use this code:
$str = 'C:\xampp\htdocs\temp\filename.tmp';
$arrayExplode = explode("\\", $str);
$file = $arrayExplode[count($arrayExplode)-1];
$filename = explode('.', $file);
$filename = $filename[0];
echo $filename;
Advice: Watch out on the path contain "n" like the first letter after the backslash. It could destroy your array.
You should use the basename function, it's meant specifically for that.
I have this string
../../some/folder/image.png
and it's possible that the string will be
../../../../../../some/folder/image.png
and I want to remove all ../ and add /root/folder/ in front of some/folder/image.png.
How do I do this?
edit
And sometime that string is placed in the middle. I mean it might be like this:
hallo hallo ../../../some/folder/image.png.
You could do the following :
$path = "hello lol ../../some/folder/image.png";
$path = preg_replace("#.*\.\./#", "", $path);
$path = "/root/folder/" . $path;
Depends where you want to use it, but you would do something like this
$path = "../../../../../some/folder/image.png";
$searchFor = substr($path, 0, strpos( $path, "some"));
$path = str_replace($searchFor, "/root/folder/", $path);
I know the preg_replace() is better, but I wanted to share a different answer
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;
How can I add divider ; in the following variable which contains string
I have string like this:
$filename = "a.jpg3c.pngyes.jpg";
I would like to have something like
a.jpg;3c.png;yes.jpg
This string is created when I select multiple files to upload.
Is regex only solution in here?
Regex is not the only solution! Perhaps you can use str_replace() instead of regex.
$filenames = "a.jpg3c.pngyes.jpg";
$img_extensions = array(".png", ".jpg", ".gif");
$semicolon_additions = array(".png;", ".jpg;", ".gif;");
$newfilenames = str_replace($img_extensions, $semicolon_additions, $filenames);
http://php.net/manual/en/function.str-replace.php
Edit: In your particular case, I would add in the semicolon at the end of the filename inside of your loop.
Here is one option using regular expressions:
$filename = "a.jpg3c.pngyes.jpg";
$regex = '/\.(jpg|png|gif)(?!$)/';
$filename = preg_replace($regex, ".$1;", $filename);
I have a path like:
$path='somefolder/foo/bar/lastdir';
and I want to remove the last part, so I have:
$path='somefolder/foo/bar';
Like I went one folder up.
I'm really newbie in php, maybe its just one function, although I can't find it anywhere.
You could try this (tested and works as expected):
$path = 'somefolder/foo/haha/lastone';
$parts = explode('/', $path);
array_pop($parts);
$newpath = implode('/', $parts);
$newpath would now contain somefolder/foo/haha.
use :
dirname(dirname('somefolder/foo/haha/lastone/somescript.php'));
this should return:
somefolder/foo/haha/
This is untested, but try:
$path_array = explode('/',$path);
array_pop($path_array);
$path = implode('/',$path_array);
If you are currently at:
somefolder/foo/haha/lastone/somescript.php
and you want to access:
somefolder/foo/haha/someotherscript.php
just type:
../someotherscript.php
Probably using a regex function would be appropriate if the last part is going to vary. Try
$pattern = '#/.*$#U';
$stripped_path = preg_replace($pattern, '', $original_path);
This will strip everything off the original path string starting from the last forward slash.
You could use a function that explodes() the $path variable into an array and then array_pop to get rid of the last element.
function path($path) {
$arrayPath = explode("/", $path);
$path = array_pop($arrayPath);
return $path = implode("/", $path);
}
The shortest variant in PHP is:
$path = preg_replace('|/[^/]*$|','', $path);
which uses a regular expression.