rename file name using path info extension with dot - php

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);

Related

PHP: How to explode string

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.

How to remove variable in img src tag?

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 to divide string in php

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);

get filename with pure extension

I'm coding a script to get image from a site. All is good, but then I notice there are some sites which have images in format like this:
http://site-name/images/dude-i-m-batman.jpg?1414151413
http://site-name/images/dude-i-m-batman.jpg?w=300
right now I'm dealing with it by doing
$file = substr($media,0, strrpos($image, '.') + 4);
I'm just wondering whether it's a good practice or there's a better way.
I've tried pathinfo and a couple other methods, but all return extension with the query string.
Thanks
Parse the URL with parse_url, retrieve the path part:
$datum = parse_url($url);
$parts = pathinfo($datum['path']);
$ext = $parts['extension'];
You may also use getImageInfo($full_url), if fopen_wrappers allow it, and retrieve image info such as width, height, and most importantly, mime_type.
This because you will find several files without extension or with the wrong one, put there to trick browsers into downloading as image and trusting that the browser will recognize the image format nonetheless (been there, done that :-( )
I'm unsure whether you mean you want the extension or (judging from your current code) the full path (minus any query string).
Here's both:
$file = "http://site-name/images/dude-i-m-batman.jpg?1414151413";
preg_match('/^([^\?]+)(?:\?.*)?/', $file, $path_noQS);
preg_match('/(?<=\.)(\w{2,5})(?:\?.*)?/', $file, $extension);
echo $path_noQS[1]; //path, without QS
echo $extension[1]; //extension
Obviously what you do now has some shortcomings. One of them you already noticed your own:
Not all URLs end with the file-extension.
Not all file-extensions are of three letters (e.g. .jpeg)
So what you want is to get the path from a URL:
$imagePath = parse_url($imageUrl, PHP_URL_PATH);
And then you want to get the extension from that path:
$imageName = pathinfo($imagePath, PATHINFO_EXTENSION);
And done. You're not the first who needs that, so functions already exist for the job.
Your solution only works with 3 character extensions. If you know all the extensions will be 3 characters than yours is a perfectly viable solution. Otherwise:
$ext = pathinfo($filename, PATHINFO_EXTENSION);
This should definitely work if you have the correct file name
If for some reason that doesn't work, you can use this:
$ext = end(explode('.', $filename));
$ext = substr(strrchr($filename, '.'), 1);
$ext = substr($filename, strrpos($filename, '.') + 1);
$ext = preg_replace('/^.*\.([^.]+)$/D', '$1', $filename);
$exts = split("[/\\.]", $filename);
$n = count($exts)-1;
$ext = $exts[$n];
may be something like this
$parsedUrl = parse_url('http://site-name/images/dude-i-m-batman.jpg?1414151413');
$parsedFileInfo = pathinfo($parsedUrl['path']);
echo $parsedFileInfo['extension'];
http://codepad.org/KXZwKCjs
$u = 'http://site-name/images/dude-i-m-batman.zip.jpg?1414151413?1234';
$u = explode('?', $u, 2 ); // ignore everything after the first question mark
$ext = end(explode('.',$u[0])); // last 'extension'

Multiple calls to preg_replace alters result

I have a bunch of files that were named in a somewhat standard format. The standard form is basically this:
[integer]_word1_word2_word3_ ... _wordn where a word could really be anything, but all words are separated by an underscore.
There is really only 3 things I want to do to the text:
1.) I want to modify the integer, which is always at the beginning, so that something like "200" would become $ 200.00.
2.) replace any "words" of the form "with", "With", "w/", or "W/" with "with".
3.) Replace all underscores with a space.
I wrote three different preg_replace calls to do the trick. They are as follows:
1.) $filename = preg_replace("/(^[0-9]+)/","$ $1.00",$filename)
2.) $filename = preg_replace("/_([wW]|[wW]ith)_/"," with ",$filename)
3.) $filename = preg_replace("/_/"," ",$filename);
Each replacement works as expected when run individually, but when all three are run, the 2nd replacement is ignored. Why would something like that occur?
Thanks for the help!
Update:
Here's the actual code I'm working with:
$path = "./img";
$dir_handle = #opendir($path);
while ($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
$id = preg_replace("/\.jpg/","",$file);
$id = preg_replace("/(^[0-9]+)/","$ $1.00", $id);
$id = preg_replace("/_([wW]\/|[wW]ith)_/"," with ", $id);
$id = preg_replace("/_/"," ", $id);
echo "<a href='javascript:show(\"img/$file\")'>$id</a> <br/>";
}
}
closedir($dir_handle);
Something like that could occur if the first replacement removes some text that the second replace matches on. But I don't think that's what is happening here. I think you just have an error in your second replacement. It looks like you are missing the /:
$filename = preg_replace("/_([wW]\/|[wW]ith)_/"," with ", $filename);
After this change it seems to work fine:
$filename = "200_word1_w/_word2";
$filename = preg_replace("/(^[0-9]+)/","$ $1.00", $filename);
$filename = preg_replace("/_([wW]\/|[wW]ith)_/"," with ", $filename);
$filename = preg_replace("/_/"," ", $filename);
print_r($filename);
Result:
$ 200.00 word1 with word2

Categories