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.
Related
$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
i need new name of file name using pathinfo($url, PATHINFO_EXTENSION);
this my code
$name = "name.txt";
$f = fopen($name, 'r');
$nwname = fgets($f);
fclose($f);
$newfname = $destination_folder .$nwname. pathinfo($url, PATHINFO_EXTENSION);
output:
1 jpeg
how to make output nospace and write (.) dot before jpeg like this
output:
1.jpeg
thank
Solved in comments, here's write up.
The . is used for concatenation. So $variable.$variable puts the values of the two variables together. $variable.'.'.$variable would add a period between the 2 variables. The trim function should be used to remove leading and trailing whitespaces from a variable.
Functional demo: https://eval.in/520038
References:
http://php.net/manual/en/function.trim.php
http://php.net/manual/en/language.operators.string.php
I think you need to concatenate strings like below :
$newfname = $destination_folder .$nwname.'.'. pathinfo($url, PATHINFO_EXTENSION);
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 have a sample code:
$filename = 'http://thebox.vn/Uploaded/catmy/2013_04_23/couple_t2.jpg?maxwidth=480';
And I using this code to remove variable (maxwidth)
echo preg_replace('/(\?)$/', '', $filename)
=> How to remove variable (maxwidth), how to fix it ?
if you want to get rid of query, just do that:
$filename = 'http://thebox.vn/Uploaded/catmy/2013_04_23/couple_t2.jpg?maxwidth=480';
$parts = explode("?",$filename);
$filename = $parts[0];
You may try this
$filename = 'http://thebox.vn/Uploaded/catmy/2013_04_23/couple_t2.jpg?maxwidth=480';
echo preg_replace("/\?[a-z]+=\d+/", '', $filename);
DEMO.
you could do:
$filename = "http://thebox.vn/Uploaded/catmy/2013_04_23/couple_t2.jpg?maxwidth=480";
$filename = array_shift(explode('?', $filename));
echo $filename;
Your current regular expression says: replace the last character of $filename with the empty string if that last character is the question mark character.
Here is a fixed regular expression that works for your particular example: /\?maxwidth=.*$/
There are many other expressions that could do the job for various circumstances. However, perhaps it would be better to use PHP's parse_url() function to split the URL into its various parts and then just discard the parts that you do not care about and merge back into a string. For example:
$filename = 'http://thebox.vn/Uploaded/catmy/2013_04_23/couple_t2.jpg?maxwidth=480';
// Parse the filename into parts.
$filename_parsed = parse_url( $filename );
// Merge the parsed filename back into a string,
// discarding any irrelevant parts.
$filename_merged = $filename_parsed[ 'scheme' ] . '://' . $filename_parsed[ 'host' ] . $filename_parsed[ 'path' ];
// Prints: http://thebox.vn/Uploaded/catmy/2013_04_23/couple_t2.jpg
echo $filename_merged;
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);